Splitting a string in Java is a common task that you may encounter when processing data. The most straightforward way to split a string is by using the split() method of the String class. In this post, we’ll explore the different ways you can use this method to split strings in Java.

Using String.split() method

Here’s an example of how you can split a string in Java:

String str = "apple,banana,cherry";
String[] arrOfStr = str.split(",");

for (String a : arrOfStr)
  System.out.println(a);

In this example, we are splitting the string str on the comma , character. The split() method returns an array of strings, which we can then loop through and print out each element.

Using String.split() method with second argument

You can also specify the maximum number of splits you want to make by providing a second argument to the split() method:

String str = "apple,banana,cherry";
String[] arrOfStr = str.split(",", 2);

for (String a : arrOfStr)
  System.out.println(a);

In this example, the split() method will only make two splits, so the output will be:

apple
banana,cherry

Using StringTokenizer

Another way to split a string in Java is by using the StringTokenizer class. This class provides a convenient way to split a string into tokens, based on a delimiter. Here’s an example:

String str = "apple,banana,cherry";
StringTokenizer st = new StringTokenizer(str, ",");

while (st.hasMoreTokens()) {
  System.out.println(st.nextToken());
}

Using the String.substring() Method:

String str = "apple,banana,cherry";
int i = str.indexOf(',');
String first = str.substring(0, i);
String second = str.substring(i + 1);
System.out.println(first);
System.out.println(second);

In this example, we’re using the indexOf() method to find the first comma in the string, and then using the substring() method to extract the two parts of the string.

Using the Pattern and Matcher Classes:

String str = "apple,banana,cherry";
Pattern pattern = Pattern.compile(",");
Matcher matcher = pattern.matcher(str);

while (matcher.find()) {
  System.out.println(str.substring(0, matcher.start()));
  str = str.substring(matcher.end());
}
System.out.println(str);

In this example, we’re using the Pattern and Matcher classes from the java.util.regex package to split the string. We first compile the pattern we want to match (in this case, a comma), and then use the Matcher class to find the occurrences of that pattern in the string.

Using the Scanner Class:

String str = "apple,banana,cherry";
Scanner scanner = new Scanner(str);
scanner.useDelimiter(",");

while (scanner.hasNext()) {
  System.out.println(scanner.next());
}
scanner.close();

In this example, we’re using the Scanner class from the java.util package to split the string. The useDelimiter() method is used to specify the delimiter to use for splitting the string. The hasNext() method returns true if there are more tokens, and the next() method returns the next token.

Using the Apache Commons Lang Library:

String str = "apple,banana,cherry";
String[] arrOfStr = StringUtils.split(str, ',');

for (String a : arrOfStr) {
  System.out.println(a);
}

In this example, we’re using the Apache Commons Lang library to split the string. The StringUtils.split() method is used to split the string on the comma , character. The library provides a lot of utility methods for working with strings, so it’s worth considering if you’re dealing with complex string manipulation tasks.

Using Java 8 Streams:

String str = "apple,banana,cherry";
List list = Arrays.asList(str.split(","));

list.stream().forEach(System.out::println);

In this example, we’re using the Streams API from Java 8 to split the string. The Arrays.asList() method is used to convert the array of strings returned by the split() method into a list. The stream() method is used to create a stream from the list, and the forEach() method is used to print each element of the stream.

These are some of the additional ways to split a string in Java. No matter which method you choose, you can easily split a string in Java to perform various string manipulation tasks.

 

 

Leave a Reply