6

I want to split a String in Android using Java. I have done this before but now I get this exception

11-20 17:57:37.665: ERROR/AndroidRuntime(25423): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISMATCHED_PAREN near index 1:
11-20 17:57:37.665: ERROR/AndroidRuntime(25423): (
11-20 17:57:37.665: ERROR/AndroidRuntime(25423):  ^

My string is like

String mystring=  "iamhere(32)";

and I want to keep only the "iamhere".

I split it using

String[] seperation = mystring.Split("(");

What am I doing wrong?

3
  • have you tried to use mystring.Split("\("); ? Commented Nov 20, 2011 at 16:05
  • I get an error when i use ("(") i tried using ("\(") and got it right :) Commented Nov 20, 2011 at 16:08
  • @user: java considers " ( "this as part of regular expression. try to use backslash to escape example : mystring.split("\("); Commented Nov 20, 2011 at 16:12

1 Answer 1

7

("\(") would be an invalid escape sequence. To escape the meaning of "(" we should be using "\\" in java.

    String mystring = "iamhere(32)";
    String[] sep = mystring.split("\\(");
    System.out.println("String after split ",sep[0]+" ");
Sign up to request clarification or add additional context in comments.

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.