Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a String in Java throws PatternSyntaxException

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?

like image 351
user878813 Avatar asked Nov 20 '11 16:11

user878813


1 Answers

("\(") 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]+" ");
like image 96
AndroGeek Avatar answered Oct 13 '22 01:10

AndroGeek