[Java] Divide a character string by a specified character

When splitting a string with the specified regular expression, use the split method.

split

Split a string with the specified regular expression

split.java


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        String str1 = "Lettuce pumpkin cucumber";
        String str2 = "Strawberry,Apple,Kiwi";
        
        //Whitespace delimited
        String[] split1 = str1.split(" "); 
        for ( int i = 0; i < split1.length; i++ ) { 
          System.out.println(split1[i]); 
        } 
        
        //Comma separated
        String[] split2 = str2.split(","); 
        for ( int i = 0; i < split2.length; i++ ) { 
          System.out.println(split2[i]); 
        } 
    }
}

result


lettuce
pumpkin
Cucumber
Strawberry
Apple
Kiwi

Recommended Posts

[Java] Divide a character string by a specified character
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] How to convert a character string from String type to byte type
[Java] How to use substring to cut out a character string
Arbitrary string creation code by Java
Java string
[Java] Character judgment / character string formatting (AOJ11 --character count)
Deletes after the specified character from the character string
Split a string with ". (Dot)" in Java
[Java] Comparison of String type character strings
[java tool] A tool that deletes files under the specified path by extension
[Java] How to use substring to cut out a part of a character string
[Java] Difference between equals and == in a character string that is a reference type
Read a string in a PDF file with Java
[Android] How to convert a character string to resourceId
[Java] String padding
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
Code to escape a JSON string in Java
Java string processing
Java character code
Split string (Java)
[Swift5] extension that allows UIImage to be specified by a String type URL
[Java] Incompatible types error occurred when assigning a character string to a char type variable
# 1_JAVA I want to get the index number by specifying one character in the character string.
[Java] Cut out a part of the character string with Matcher and regular expression
Search for character string B from character string A (duplicate count)
[Java] How to get a request by HTTP communication
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Create a filter
[Java] String comparison and && and ||
Java string multiple replacement
Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
java build a triangle
[Note] Java: String search
[Note] Java: String survey
About Java String class
I want to find out if the specified character string is supported by the target character code
Update your Java knowledge by writing a gRPC server in Java (2)
Convert a Java byte array to a string in hexadecimal notation
Dynamically access the property specified by the string in Spring BeanWrapper
Update your Java knowledge by writing a gRPC server in Java (1)
How to store a string from ArrayList to String in Java (Personal)
How to select a specified date by code in FSCalendar
I tried to convert a string to a LocalDate type in Java
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
About the method to convert a character string to an integer / decimal number (cast data) in Java