7

I have a string like ||81|||01|| and I want to split the string with | symbol.

I had done this way,

String str = "||81|||01||";
System.out.println(str .split("\\|").length); //printing 6 . But I am expecting 8

what is wrong with this code? | How can I split this string with that character so that I will get expected length (8)?;

Dinoop paloli
  • 633
  • 2
  • 8
  • 25

6 Answers6

11

Using split("\\|") is the same as split("\\|", 0), where the limit parameter 0 tells the function "omit trailing empty strings". So you are missing the last two empty strings. Use the two-argument version and supply a negative number to obtain all parts (even trailing empty ones):

str.split("\\|", -1)
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
8

Print:

System.out.println(Arrays.toString(str.split("\\|")));

And you'll understand why it's printing 6.

You can try doing what you want using public String[] split(String regex, int limit):

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

So you should do:

System.out.println(str.split("\\|", -1).length);

Now, printing the array will print:

[, , 81, , , 01, , ] as you expected.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

You can also use string.split(Pattern.quote("|"),-1) for spliting a string on a special character.

Santosh
  • 2,093
  • 1
  • 15
  • 21
  • 1
    Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone May 04 '15 at 09:17
  • example to split "||81|||01||" based on the special character '|' then you can use `String s="||81|||01||"; String array[]=s.split(Pattern.quote("|"),-1) ` length of the array thus obtained will be 8. – Santosh May 05 '15 at 05:32
1

You need to use:

str.split("\\|", -1)

The second parameter is limit. From the javadoc:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. 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. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Lucas
  • 14,227
  • 9
  • 74
  • 124
1

str.split("\\|", -1) will do the necessary. Possible duplicate : Here

Community
  • 1
  • 1
javadev
  • 1,639
  • 2
  • 17
  • 35
1

String str = "||81|||01||"; System.out.println(str.split("\\|", 8).length);

The second argument to split specifies maximum number of matches. Single argument split is like invoking split(str, 0) which leaves out trailing strings. See javadoc of both for more explaination.

Datta
  • 91
  • 5
  • using `8` is quite inflexible. if you just use a negative argument (like the other answers), you'll simply get all parts, no matter how many `|` there are in `str` – Martin Ender Apr 30 '13 at 10:38