4

I want split a string like this:

  C:\Program\files\images\flower.jpg     

but, using the following code:

  String[] tokens = s.split("\\");
  String image= tokens[4];

I obtain this error:

 11-07 12:47:35.960: E/AndroidRuntime(6921): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_ESCAPE_SEQUENCE near index 1:
3
  • But I want split the Original String in 5 parts: C: Program files images flower.jpg Commented Nov 7, 2012 at 11:56
  • @Joseph82 whatever Esailija is saying will work for you Commented Nov 7, 2012 at 11:59
  • duplicate stackoverflow.com/questions/4025482/… Commented Nov 7, 2012 at 12:00

7 Answers 7

5

try

String s="C:\\Program\\files\\images\\flower.jpg"

String[] tokens = s.split("\\\\");

In java(regex world) \ is a meta character. you should append with an extra \ or enclose it with \Q\E if you want to treat a meta character as a normal character.

below are some of the metacharacters

<([{\^-=$!|]})?*+.>

to treat any of the above listed characters as normal characters you either have to escape them with '\' or enclose them around \Q\E

like:

        \\\\ or \\Q\\\\E
Sign up to request clarification or add additional context in comments.

1 Comment

@Joseph82, the reason it works is because a Regex pattern starts and ends with the \ sign, and you need to escape the special char \ so you start and end the pattern with the \ sign, and then the pattern itself is the special char (with escaping) \\. all in all it's \\\\
2

You need to split with \\\\, because the original string should have \\. Try it yourself with the following test case:

    @Test
public void split(){
      String s = "C:\\Program\\files\\images\\flower.jpg";     


        String[] tokens = s.split("\\\\");
        String image= tokens[4];
        assertEquals("flower.jpg",image);
}

Comments

1

There is 2 levels of interpreting the string, first the language parser makes it "\", and that's what the regex engine sees and it's invalid because it's an escape sequence without the character to escape.

So you need to use s.split("\\\\"), so that the regex engine sees \\, which in turn means a literal \.

If you are defining that string in a string literal, you must escape the backslashes there as well:

String s = "C:\\Program\\files\\images\\flower.jpg";     

Comments

1

String[] tokens=s.split("\\\\");

Comments

1

Try this:

String s = "C:/Program/files/images/flower.jpg";
String[] tokens = s.split("/");
enter code hereString image= tokens[4];

Comments

0

Your original input text should be

 C:\\Program\\files\\images\\flower.jpg  

instead of

 C:\Program\files\images\flower.jpg  

1 Comment

I get the String from a web service. So I insert the path in the string in this way (using Ksoap2): s = response.getProperty("image").toString();
0

This works,

    public static void main(String[] args) {
        String str = "C:\\Program\\files\\images\\flower.jpg";
        str = str.replace("\\".toCharArray()[0], "/".toCharArray()[0]);
        System.out.println(str);
        String[] tokens  = str.split("/");
        System.out.println(tokens[4]);      
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.