Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How split() method works in java?

Tags:

java

string

split

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", "", ""}. According to me there should be 4 substrings:{"geek", "","@for@geek", ""}. Can someone clarify my doubt?

like image 329
Ashutosh Tiwari Avatar asked Oct 08 '17 10:10

Ashutosh Tiwari


People also ask

How does split method work in Java?

The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.

How does split method work?

The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

Why did we use the split () method?

Whenever there is a need to break bigger strings or a line into several small strings, you need to use the split() function in Python. The split() function still works if the separator is not specified by considering white spaces, as the separator to separate the given string or given line.

What is the use of split () function explain with example?

Split function returns a list of strings after dividing the string based on the given separator. Following are the advantages of using a split function in python: At some point we may have to break down a large string into smaller chunks or strings. It is the opposite of concatenation, which adds two strings together.


1 Answers

If you look carefully at the docs:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

So your resulting array contains two things:

  • a substring of your string which is followed by s (italic part)
  • whatever is left at the end of your string (bold part)

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

Note that the limit argument of 5 you passed is also related. As per the docs:

If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

So the last matched delimiter is the s at the very end. After that there is still an empty string that it haven't checked.

like image 104
Sweeper Avatar answered Sep 29 '22 13:09

Sweeper