Java String Split: Complete Guide
In Java, the split() method can be used to divide a string. The split() method takes a regular expression as a delimiter, and returns an array of strings, with elements consisting of substrings split according to the delimiter.
Here is an example code demonstrating how to split a string using the split() method.
String str = "Hello,World";
String[] parts = str.split(",");
for (String part : parts) {
System.out.println(part);
}
When running the code above, the output will be:
Hello
World
In this example, we are splitting the string “Hello,World” using a comma as a delimiter to create a string array with two elements. Next, we iterate through the string array using a for-each loop and print out each element.