Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string split function acting strange

Tags:

java

string

split

I am noticing strange behaviour when using the split() method in Java.

I have a string as follows: 0|1|2|3|4|5|6|7|8|9|10

String currentString[] = br.readLine().split("\\|");
System.out.println("Length:"+currentString.length);
for(int i=0;i < currentString.length;i++){
     System.out.println(currentString[i]);
}

This will produce the desired results:

Length: 11
0
1
2
3
4
5
6
7
8
9
10

However if I receive the string: 0|1|2|3|4|5|6|7|8||

I get the following results:

Length: 8
0
1
2
3
4
5
6
7
8

The final 2 empties are omitted. I need the empties to be kept. Not sure what i am doing wrong. I have also tried using the split in this manner as well. ...split("\\|",-1);

but that returns the entire string with a length of 1.

Any help would be greatly appreciated!

like image 496
astro Avatar asked Jan 11 '10 03:01

astro


People also ask

Does split () alter the original string?

Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.

What happens when you split a string in Java?

The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

Is string Tokenizer faster than 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.


Video Answer


3 Answers

The default behavior of split is to not return empty tokens (because of a zero limit). Use the two parameter split method with a limit of -1 will give you all empty tokens in the return.

UPDATE:

Test code as follows:

public class Test {
    public static void main(String[] args) {
    String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|", -1);
    System.out.println("Length:"+currentString.length); 
    for(int i=0;i < currentString.length;i++){ System.out.println(currentString[i]); }
  }
}

Output as follows:

Length:11
0
1
2
3
4
5
6
7
8
--- BLANK LINE --    
--- BLANK LINE --

The "--- BLANK LINE --" is put in by me to show that the return is blank. It is blank once for the empty token after 8| and once for the empty trailing token after the last |.

Hope this clears things up.

like image 58
Gennadiy Avatar answered Oct 06 '22 13:10

Gennadiy


String.split() is weird.

Its extreme weirdness, in this and other ways, are some of the reasons why we made Splitter.

It has less surprising behavior and lots of flexibility.

like image 21
Kevin Bourrillion Avatar answered Oct 06 '22 14:10

Kevin Bourrillion


My Java is a little bit rusty, but shouldn't it be:

String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|");
System.out.println("Length:"+currentString.length); 
for(int i = 0; i < currentString.length; i++)
{
  System.out.println(currentString[i]); 
} 
like image 40
Paulo Santos Avatar answered Oct 06 '22 15:10

Paulo Santos