chevron_left
0 points
8 4 4

 

In this article, we will know about how to split a string in java. Java String split method is used to split a string into substrings by limiting given or regular expressions.

As an example:

String: sotan @ syrup

Regular Expression: @

Output: { "sotan", "syrup"}

How To Split Java String

We have two variants) split method (in class String.

1. String [ ] split (String regex): This returns an array of strings after splitting a String input based on regular expressions restrictions.

2. String [] split (String regex, int limit): Java String split method is used when we want to substring is restricted. The only difference between this method and the above method is that it limits the number of strings back after break up. For example, split ( "anydilimiter", 3) will return array of only three strings even if the string delimiter is present in more than three times.

If the limit is (-ve) then the returned array will have a lot of substring possible but when the limit is 0 then the returned array will have all the substring does not include left empty Strings.

PatternSyntaxException throw if the regular expression syntax that is not valid.

Java String split Example

public class SplitExample {

   public static void  main ( String args [ ] ) {

// this is input String

String str = new String ( "11/10/2020");



System.out.println ( "split (String regex):");

/ * Here we use the first variation of the java method splits rope

* The split strings into substrings by ordinary

* The expression, there is no limit on the substring

* /

String array1 [] = str.split ( "/");

for (String temperature: array1) {

System.out.println (temp);

}



/ * Using a split second variation method here. Because the limit is passed

* As 2. This method will only produce two substring.

* /

System.out.println ( "split (String regex, int limit) with a limit = 2");

String array2 [] = str.split ( "/", 2);

for (String temperature: array2) {

System.out.println (temp);

}



System.out.println ( "split (String regex, int limit) with a limit = 0");

Array3 String [] = str.split ( "/", 0);

for (String temperature: array3) {

System.out.println (temp);

}



/ * When we crossed the line negative. Split method in cooperation with the first variation

* Due to the negative limit says that the method returns a substring indefinitely.

* /

System.out.println ( "split (String regex, int limit) with a limit = -5:");

Array4 String [] = str.split ( "/", -5);

for (String temperature: array4) {

System.out.println (temp);

}

   }

}

Output:

split (String regex):

11

10

2020

split (String regex, int limit) with a limit = 2:

11

10/2020

split (String regex, int limit) with a limit = 0:

11

10

2020

split (String regex, int limit) with a limit = -5:

11

10

2020

 

More Posts

How to split a string by spaces in java MallikaShaik672 - Nov 4, 2020
HOW TO REMOVE SUBSTRING FROM STRING IN PYTHON jaseera - Oct 23, 2020
Split a string with spaces in java DivyaN - Aug 15
read a text file line by line and split words in java DivyaN - Aug 19
How to delete a char from string java MallikaShaik672 - Nov 1, 2020
given a string, return a string where for every char in the original, there are two chars. - Java DivyaN - Sep 3
get the first letter of a string in java DivyaN - Aug 16
Python split a string in half Naymul01 - Oct 5
what is the default initial value of a string? James Steve - Oct 22, 2020
split a string into an array javascript DivyaN - Aug 16