How does the split () method work in java?

My question is why the following program:

// Java program to demonstrate working of split(regex, // limit) with high limit. public class GFG { public static void main(String args[]) { String str = " geekss@for @geekss"; String [] arrOfStr = str.split("s", 5); } } 

splits the string " geekss@for @geekss" into 5 substrings: {"geek", "", "@ for@geek ", "", ""} . For me, there should be 4 substrings: {"geek", "","@ for@geek ", ""} . Can someone clarify my doubts?

+5
source share
4 answers

If you look closely at the docs:

The array returned by this method contains each substring of this string that ends with another substring that matches the given expression, or ends at the end of the string .

So your resulting array contains two things:

  • substring of your string followed by s (italic part)
  • all that remains at the end of the line (bold)

The reason you got {"geek", "", "@ for@geek ", ""} for the first four elements is because they are followed by s . The last "" you received is what is left after each s .

Note that the limit 5 argument you passed is also related. According to the docs:

If the limit n is greater than zero, the pattern will be applied no more than n - 1 times, the length of the array will be no more than n , and the last element of the array will contain all the input data for the last matched separator.

So the last matched delimiter is s at the very end. After that, there is still an empty line that she did not check.

+4
source

Here is an illustrated explanation of what is happening:

 ^geek ss @ for@geek ss $ geek |""| @ for@geek |""|"" 

The position between the final delimiter and $ is considered as an empty string. Note that if your line starts with s , you will also get an empty line.

The trivial reason we see 5 matches is because your separation limit is greater than or equal to 5, but this does not explain the result.

+2
source

Split("s", 5) finds 4 s-characters and returns 5 subtypes that exist between the two sequences 's' , thus empty substrings or from the beginning of the line to the first and from the last to the end of the line. So you have 5 substrings.

If you look at @tobias_k for a very useful comment, you have 4 's' chars, not 5, the second parameter (number 5) in the split method indicates how many substrings are there if we remove s within 4 s 5 if you try with any number greater than 5, you will get the same results, since there are no other substrings to separate.

+1
source

Your line is "geekss @for @geekss", so if you consider "s" as your separator, you get:

"geek" + ['s'] + "+ ['s'] +" @for @geek "+ ['s']" + ['s'] + ""

So, you have ["geek", "", "@ for@geek ", "", ""]

Perhaps the part that confuses you is the last empty line. Think that the delimiter is always "contained" in a string. Therefore, if this happens at the end, it is actually considered "your_string" + your_delimiter + "" .

The same thing happens if your line starts with "s": in this case, the first element of your split array will be "".

0
source

Source: https://habr.com/ru/post/1272434/


All Articles