Well I want to call a
String newString = oldString.replaceAll("}","");
but I’m getting error with the } I have tried with:
String newString = oldString.replaceAll("\\}\\","");
String newString = oldString.replaceAll("\}\","");
String newString = oldString.replaceAll("//}//","");
String newString = oldString.replaceAll("/}/","");
and none of them works. How could I do that?
Thanks
Here’s the error:
10-19 12:17:44.907: W/System.err(7030): java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
10-19 12:17:44.907: W/System.err(7030): }
10-19 12:17:44.907: W/System.err(7030): ^
10-19 12:17:44.907: W/System.err(7030): at java.util.regex.Pattern.compileImpl(Native Method)
10-19 12:17:44.907: W/System.err(7030): at java.util.regex.Pattern.compile(Pattern.java:400)
10-19 12:17:44.907: W/System.err(7030): at java.util.regex.Pattern.<init>(Pattern.java:383)
10-19 12:17:44.907: W/System.err(7030): at java.util.regex.Pattern.compile(Pattern.java:374)
10-19 12:17:44.907: W/System.err(7030): at java.lang.String.replaceAll(String.java:1784)
10-19 12:17:44.907: W/System.err(7030): at com.rotaryheart.MainActivity$1.onClick(MainActivity.java:70)
10-19 12:17:44.907: W/System.err(7030): at android.view.View.performClick(View.java:4084)
10-19 12:17:44.907: W/System.err(7030): at android.view.View$PerformClick.run(View.java:16966)
10-19 12:17:44.907: W/System.err(7030): at android.os.Handler.handleCallback(Handler.java:615)
10-19 12:17:44.907: W/System.err(7030): at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 12:17:44.907: W/System.err(7030): at android.os.Looper.loop(Looper.java:137)
10-19 12:17:44.907: W/System.err(7030): at android.app.ActivityThread.main(ActivityThread.java:4940)
10-19 12:17:44.907: W/System.err(7030): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 12:17:44.907: W/System.err(7030): at java.lang.reflect.Method.invoke(Method.java:511)
10-19 12:17:44.915: W/System.err(7030): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-19 12:17:44.915: W/System.err(7030): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-19 12:17:44.915: W/System.err(7030): at dalvik.system.NativeStart.main(Native Method)
and this is the line MainActivity.java 70
String newString = oldString.replaceAll("}","");
Well this is my onClick call
go.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
try {
oldString= "} test } for }";
Toast.makeText(getApplicationContext(), "Test for }",
Toast.LENGTH_SHORT).show();
String newString = oldString.replaceAll("}", "");
Toast.makeText(getApplicationContext(), ""+newString , Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Well your first code should work fine. You don’t need to escape your
}.However, you do need to escape opening braces –
{, if you are using it.So the code: –
works fine. The problem you are getting might be because of something you are hiding from us.
If you have some other regex than the one shown above, then we can’t see exactly what the problem is.
Ok, I tried it with your given string: –
OUTPUT: –
As you see I am getting the requried output, but can’t understand why this is not working in your code.
But still, you can try using
replace()method, and see if it works: –