-1

I need split a string using ';' as separator, if the string has all of fields filled it's works good, but if some fields are not filled, like string.split("A;B;C;;;") not work... for this string I expected that output would

[0]=A

[1]=B

[2]=C

[3]=''

[4]=''

[5]=''

, but the output is only first three fields

[0]=A

[1]=B

[3]=C

... the other fields wasn't created

Some clue how to solve this?

Community
  • 1
  • 1

2 Answers2

1

The ; character seperates C from the end of the string, no matter how many of them there are. The String.split() method will not return plain white space or an empty string.

Logan Rodie
  • 673
  • 4
  • 12
0

If not mistaken, split looks for characters[ASCII] between the two separators and in case of

str.split("A;B;C;;;"),

there are no characters between the two semi colons. Split by defaults remove the empty string, to overrule that we need to use the overloaded split as detailed here in Java docs.

Try this if possible based on your input architecture:

String str = "A;B;C;;;";
str.split(";", -1);

This helps to look even for null string output would be

[0] = "A"
[1] = "B"
[2] = "C"
[3] = ""
[4] = ""

Hope this helps.

sumandas
  • 555
  • 7
  • 20