You can split a string in Java using the String.split() method, which takes a regular expression as its argument and returns an array of substrings. Here's an example:
java
public class StringSplitExample {
    public static void main(String[] args) {
        String text = "Hello,world,Java";
        
        // Split the string using a comma as the delimiter
        String[] parts = text.split(",");
        
        for (String part : parts) {
            System.out.println(part);
        }
    }
}
In this example, the split method is used to split the text string using a comma , as the delimiter. The resulting array parts contains the substrings.
When you run the code, you'll see the output:
Hello world Java
Keep in mind that the argument to split() is a regular expression, so special characters in the delimiter need to be properly escaped if necessary. For more complex splitting scenarios, you can use regular expressions to define custom delimiters and handling options.
Comments
Post a Comment