Vinay Sharma
Vinay Sharma

Reputation: 1303

Why split method does not support $,* etc delimiter to split string

import java.util.StringTokenizer;
class MySplit
{
  public static void main(String S[])
  {
    String settings = "12312$12121";
    StringTokenizer splitedArray = new StringTokenizer(settings,"$");

    String splitedArray1[] = settings.split("$");
        System.out.println(splitedArray1[0]);

    while(splitedArray.hasMoreElements())
        System.out.println(splitedArray.nextToken().toString());            
  }
}

In above example if i am splitting string using $, then it is not working fine and if i am splitting with other symbol then it is working fine.

Why it is, if it support only regex expression then why it is working fine for :, ,, ; etc symbols.

Upvotes: 4

Views: 1334

Answers (5)

Sohail
Sohail

Reputation: 4586

Because $ is a special character used in Regular Expressions which indicate the beginning of an expression.

You should escape it using the escape sequence \$ and in case of Java it should be \$

Hope that helps. Cheers

Upvotes: 1

Maroun
Maroun

Reputation: 95998

$ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the string "$", but as the special meta character $. One sexy solution is:

settings.split(Pattern.quote("$"))

Pattern#quote:

Returns a literal pattern String for the specified String.

... The other solution would be escaping $, by adding \\:

settings.split("\\$")

Important note: It's extremely important to check that you actually got element(s) in the resulted array.

When you do splitedArray1[0], you could get ArrayIndexOutOfBoundsException if there's no $ symbol. I would add:

if (splitedArray1.length == 0) {
    // return or do whatever you want 
    // except accessing the array
}

Upvotes: 6

Rahul Tripathi
Rahul Tripathi

Reputation: 172568

Dollar symbol $ is a special character in Java regex. You have to escape it so as to get it working like this:

settings.split("\\$");

From the String.split docs:

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

On a side note:

Have a look at the Pattern class which will give you an idea as to which all characters you need to escape.

Upvotes: 2

npinti
npinti

Reputation: 52185

The problem is that the split(String str) method expects str to be a valid regular expression. The characters you have mentioned are special characters in regular expression syntax and thus perform a special operation.

To make the regular expression engine take them literally, you would need to escape them like so:

.split("\\$")

Thus given this:

String str = "This is 1st string.$This is the second string";
        for(String string : str.split("\\$"))
            System.out.println(string);

You end up with this:

This is 1st string.
This is the second strin

Upvotes: 2

mcamier
mcamier

Reputation: 426

If you take a look at the Java docs you could see that the split method take a regex as parameter, so you have to write a regular expression not a simple character.

In regex $ has a specific meaning, so you have to escape it this way:

settings.split("\\$");

Upvotes: 3

Related Questions