views:

72

answers:

2

I want to find position in( position within string, but I can not explicitly write:

int index = valueOf("in(");

because I get error of non closed group ( java.util.regex.PatternSyntaxException Unclosed group). If I escape parenthesis it will not find index of in(, but in\(

int index = value.indexOf("in\\(");

How can I achieve that?

EDIT: I indeed made an mistake in forming the question. In my code i use indexOf but the real problem was an String.split. But when I got PatternSyntaxException I somehow went to line where indexOf was, thus I started to fixing the issue in wrong place. When I escaped ( then indexOf did not find the "\(".

What I got confused and felt like loosing ground was what the eclipse did, then I post stackoverflow, what I do:

 String value = "in(test";
 String[] arr = value.split("in\\(");

so I have arr = {"","test"} as below: alt text

but why? why I see value: alt text

Why in array elements string value is shown as original string? Is it bug or feature?

+1  A: 

String.indexOf does not take a regex. You should be able to use:

int index = value.indexOf("in(");
Grodriguez
A: 

String.indexOf essentially calls,

static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex); 

and this doesn't use regular expression. It basically does a String search....

Therefore,

int index = value.indexOf("in("); 

will return you the first occurrence index (from position 0) where in( occurs, else a -1.

The Elite Gentleman