tags:

views:

108

answers:

3

I want to remove a string that is between two characters and also the characters itself , lets say for example:

i want to replace all the occurrence of the string between "#?" and ";" and remove it with the characters.

From this

"this #?anystring; is  #?anystring2jk; test"

To This

"this is test"

how could i do it in java ?

A: 

string.replaceAll(start+".*"+end, "")

is the easy starting point. You might have to deal with greediness of the regex operators, however.

Carl
+2  A: 

Use regex:

myString.replaceAll("#\?.*?;", "");
Computerish
Worked like a charm, Thanks !
Fevos
+1  A: 

@computerish your answer executes with errors in Java. The modified version works.

myString.replaceAll("#\\?.*?;", "");

The reason being the ? should be escaped by 2 backslashes else the JVM compiler throws a runtime error illegal escape character. You escape ? characters using the backslash .However, the backslash character() is itself a special character, so you need to escape it as well with another backslash.

A_Var
I don't understand how the answer by computerish has 2 votes. Is it fixed or what???.
A_Var