Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split method behavior

Tags:

java

string

split

I don't see why does the following output makes sense.

String split method on an empty String returning an array of String with length 1

String[] split = "".split(",");
System.out.println(split.length);
Returns array of String with length 1

String[] split = "Java".split(",");
System.out.println(split.length);
Returns array of String with length 1

How to differentiate??

like image 817
Srujan Kumar Gulla Avatar asked Aug 27 '12 20:08

Srujan Kumar Gulla


People also ask

What does split method of string do?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

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.

Is string split efficient?

String. split(String) won't create regexp if your pattern is only one character long. When splitting by single character, it will use specialized code which is pretty efficient.

What is the difference between StringTokenizer and split?

The split() method is preferred and recommended even though it is comparatively slower than StringTokenizer. This is because it is more robust and easier to use than StringTokenizer. A token is returned by taking a substring of the string that was used to create the StringTokenizer object.


3 Answers

From the documentation:

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.

To answer your question, it does what it is expected to do: the returned substring is terminated by the end of the input string (as there was no , to be found). The documentation also states:

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

Note that this is a consequence of the first statement. It is not an additional circumstance that the Java developers added in case the search string could not be found.

like image 99
Simeon Visser Avatar answered Oct 26 '22 23:10

Simeon Visser


I hit this, too. What it's returning is the string up to but not including the split character. If you want to get no strings, use StringTokenizer:

StringTokenizer st = new StringTokenizer(someString,',');
int numberOfSubstrings = st.countTokens();
like image 20
Chris Gerken Avatar answered Oct 26 '22 23:10

Chris Gerken


It's returning the original string (which in this case is the empty string) since there was no , to split on.

like image 38
Jeff Storey Avatar answered Oct 26 '22 22:10

Jeff Storey