how to replace single backslash with empty character in string? - android

I want to replace single backslash with empty character from string.
when i use syntax :- message = message.replaceAll("\",""); then it gives me error when i use message = message.replaceAll("\"",""); this syntax then it does not have any effect

Back Slash itself is an escape Character,so you need to use double backslash to achieve it,as in
message=message.replaceAll("\\","");

Use message.replaceAll("\\",""); instead

It should be either:
message = message.replaceAll("\\\\","");
Or
message = message.replace("\\","");

Related

Android "()" not treated as string

I have a problem with one line of code, which goes like this:
string = string.replaceAll("sin()", "");
As you can see, in a string, all "sin()" need to be replaced with "". But the problem is that () is not treated as string and so this line of code replaces "sin()" with "()". Moreover, Android studio reports warning on the () saying empty group. I tried solving this with escape character, but that doesn't work. Would following code work by any chance?
String compare = "sin()";
string = string.replaceAll(compare, "");
replaceAll's first parameter is a regular expression, of which ( and ) are special characters. You would instead need to use
string = string.replaceAll("sin\\(\\)", "");
Note the use of \\ - \ is actually a special character in Java strings, so you must first escape the \ by using \\.
String z = "sin() is equal sin()";
Log.d("TEST",z);
z = z.replaceAll("sin\\(\\)", "");
Log.d("TEST",z);
Gives following output:
sin() is equal sin()
is equal
You need to separate your ( and ) because its a special charaters
So you need to use "\\"
example:
string = string.replaceAll("sin\\(\\)", "");

remove only double quotes from string before and after square bracket

I am trying to remove double quotes before square bracket like "[
and I am using following code to do it but it says illegal escape charecter.
str = str.replace("\[","[");
I want to remove only double quotes ie " which is only before square bracket ie [. Please guide me.
You can use:
str = str.replaceAll("\"\\[", "[");
Both replace() and replaceAll() do the job. Using replace, you don't have to cope with regular expressions. Don't get confused by the name. In fact replace replaces all occurrences, not just the first.
str = str.replace("\"[", "[");

Android How to Remove( " )Character from String

I wanted to to Remove ( " ) Character from String
This is my text
"69452;6486699"
I need to Have This Text
69452;6486699
I've tryed to use String.Replace
text = text.replace(""","");
and does not work
Also I've Use This Way
text = text.replace("/"","");
But Not Again
Any One Can Help me ?!
use this code
text.replace("\"", "");
Backslash () is used for escaping special characters, forward slash (/) is just a regular character with no special meaning in the string.
Wrong slash. Do it with a backslash:
text = text.replace("\"", "");
It's
text = text.replace("\"", "");
Backslash (\) is used for escaping special characters, forward slash (/) is just a regular character with no special meaning.
You need to try like this
text = text.replace("\"", "");
Look into String.replace()

replaceAll function not working while removing special character

I am working with a project where some data are retrieved by JSON parsing. Unfortunately, invalid character '\' escapes. I need to remove them. I tried calling .replace("\\'","\");. This solution is not working. No exceptions are thrown, but the string does not change. Here is my code:
shop_name = c.getString(TAG_SHOP_NAME);
if(shop_name.contains("\\'")==true)
{
//try{
shop_name=shop_name.replaceAll(Pattern.quote("\\'"), "'");
Log.e("vvvvvv","new shop name: "+shop_name);}
//catch(Exception q){Log.e(TAG+" vvvv","EXPTN",q);}
}
send JSON object is: Bimal\'s
required object: Bimal's
Please let me know whether I went wrong somewhere or if there is any other method other than replaceAll.
You need to double escape the backslash as it's an escape character in both strings and regex:
shop_name.replaceAll("\\\\'", "'");
Or without using regex (as it's not needed in this circumstance):
shop_name.replace("\\'", "'");
Escape the meta character " with "\" :
string.replaceAll("\"", "");
Remember to assign it back to the String reference , because it returns a new String object.
You should use replace() instead:
str = str.replace("\"", "");
replaceAll() is used for replacing regular expressions.

PatternSyntaxException: String.replaceAll() android

I want to remove all { } as follow:
String regex = getData.replaceAll("{", "").replaceAll("}", "");
but force close my app with log.
java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX
what have i done wrong ?
You need to escape {:
String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "");
Curly brackets are used to specify repetition in regex's, therefore you will have to escape them.
Furthermore, you should also consider removing all the brackets in one go, instead of called replaceAll(String, String) twice.
String regex = getData.replaceAll("\\{|\\}", "");
For what you want to do you don't need to use a regex!
You can make use of the replace method instead to match specific chars, which increases readability a bit:
String regex = getData.replace("{", "").replace("}", "");
Escaping the \\{ just to be able to use replaceAll works, but doesn't make sense in your case

Categories

Resources