Splitting a string into substrings in Java.

public class SplitString {

public static void main(String[] args) {

// Example string

String originalString = "apple,banana,grape,orange";

// Split the string into substrings using comma (,) as delimiter

String[] substrings = originalString.split(",");

// Print each substring

System.out.println("Original string: " + originalString);

System.out.println("Number of substrings: " + substrings.length);


for (String substring : substrings) {





In this example, the original string `"apple,banana,grape,orange"` is split into substrings using the comma `,` as the delimiter. The resulting substrings are stored in the `substrings` array. Finally, the program prints each substring.

You can replace the comma `,` with any other character or regular expression pattern to split the string based on different criteria.