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()
Related
I'm doing Firebase RemoteConfig integration. In one of the scenarios, I need to break a text line, so I tried to use new line character (\n).
But this is not working, it is neither displaying as an extra character nor creating another line.
My solution is replace \n manually (assuming that in Firebase Console you put property for TITLE as "Title\nNewLine"):
FirebaseRemoteConfig.getInstance().getString(TITLE).replace("\\n", "\n")
Try using an uncommon character like two pipes || and then replacing every occurance of those with a newline after you do getString() in the code.
You can insert encoded text(with Base64) to Firebase panel.
After, decode the String from your Java class and use it.
Like
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
The trick (which actually works for all HTML tags supported on your target platform) is to wrap the String in a JSON Object on RemoteConfig, like so:
{
"text":"Your text with linebreaks...<br><br>...as well as <b>bold</b> and <i>italic</i> text.
}
On the target platform you then need to parse the JSON and convert it back to a simple string. On Android this looks like this:
// extract value from JSON
val text = JSONObject(remoteConfig.getString("remoteConfig_key")).getString("text")
// create Spanned and use it
view.text = HtmlCompat.fromHtml(text)
So what worked for me is to use "||" (or some other character combination you are confident will not be in the string) as the new line character. Then replace "||" with "\n". This string will then display properly for me.
For some reason sending "\n" in the string doesn't get recognized as expected but adding it manually on the receiving side seems to work.
To make the suggestion mentioned above, you can try this code(that can be generalized to "n" number of elements). Simply replace the sample text with yours with the same format and add the amount of elements
String text="#Elemento1#Elemento2#Elemento3#";
int cantElementos=3;
arrayElementosFinales= new String[cantElementos];
int posicionNum0=0;
int posicionNum1;
int posicionNum2;
for(int i=0;i<cantElementos;i++){
posicionNum1=text.indexOf("#",posicionNum0);
posicionNum2=text.indexOf("#", posicionNum1+1);
char [] m = new char[posicionNum2-posicionNum1-1];
text.getChars(posicionNum1+1, posicionNum2,m,0);
arrayElementosFinales[i]=String.valueOf(m);
posicionNum0=posicionNum2;
}
Use Cdata in the remote config in combination with "br" tag and HTML.fromHtml() .. for eg.
<![CDATA[ line 1<br/>line 2]]>
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\\(\\)", "");
I have a string which contains space like this
https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_e872fd49-0dab-4502-8689-d126b9552334/Sales Force Technologies/360/SMC_C1_M1_Agile_Overview_and_need_for_Agile.mp4?
How to replace spaces in this string with %20 so that it should display
Sales%20Force%20Technologies
I want to replace only space not other special characters.
Use the String.replaceAll() method.
String newUrlString = urlString.replaceAll(" ", "%20");
Beware though that the first parameter is a regex expression, so if you want to split with a . (or any special regex char for that matter) you would have to do this \.
Im trying to use a question mark as a variable for a string.
I've tried...
strings.xml
<string name="questionMark">\?</string>
.class
String questionMark;
questionMark = getResources().getString(R.string.questionMark);
String delim4 = (questionMark);
This causes a fource close regex error.
and
String delim4 = (\?);
This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
and also
I've tried putting 2 backslashes in front of it
String delim4 =(\\?)
System.out.println("delim "+ delim4);
But that just escapes the second slash and sometimes force closes as well.
the output for that was
delim \?
Can any tell me how to put in the question mark as the string. I'm using it as variable to spit a string. The String Im splitting can not be changed.
plz help
Edit added split code
if (FinishedUrl.contains(questionMark)){
String delim3 = (".com/");
String[] parts3 = FinishedUrl.split(delim3);
String JUNK3= parts3[0];
String fIdStpOne = parts3[1];
String fIdStpTwo = fIdStpOne.replaceAll("=#!/","");
String delim4 = (questionMark);
String[] parts4 = fIdStpTwo.split(delim4);
String fIdStpThree= parts3[0];
String JUNK4 = parts3[1];
FId = fIdStpThree;
}
As pointed out by user laalto, ? is a meta-character in regex. You must work around that.
Let's see what's happening here. Firstly, some ground rules:
`?` is not a special character in Java
`?` is a reserved character in regex
This entails:
String test = "?"; // Valid statement in java, but illegal when used as a regex
String test = "\?"; // Illegal use of escape character
Why is the second statement wrong? Because we are trying to escape a character that isn't special (in Java). Okay, we'll get back to this.
Now, for the split(String) method, we need to escape the ? - it being a meta-character in regex. So, we need \? for the regex.
Coming back to the string, how do we get \?? We need to escape the \(backslash) - not the question mark!
Here's the workflow:
String delim4 = "\\?";
This statement gives us \? - it escapes the \(backslash).
String[] parts4 = fIdStpTwo.split(delim4);
This lets us use \? as a regex in the split() method. Since delim4 is being passed as a regex, \? is used as ?. Here, the prefix \ is used to escape ?.
Your observations:
String delim4 = (\?);
This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
I covered this above. You are escaping ? at the java level - but it isn't a special character and needs no escaping - hence the error.
String delim4 =(\\?)
System.out.println("delim "+ delim4);
But that just escapes the second slash and sometimes force closes as well. the output for that was
delim \?
This is what we want. It is easier to think of this as a two stage process. The first stage deals with successfully placing a \(backslash) in front of the ?. In the second stage, regex finds that the ? has been prefixed by a \ and uses ? as a literal instead of a meta-character.
And here's how you can place the regex in your res/values/strings.xml:
<string name="questionMark">\\?</string>
By the way, there's another option - not something I use on a regular basis these days - split() works just fine.
You can use StringTokenizer which works with delimiters instead of regex. Afaik, any literal can be used as a delimiter. So, you can use ? directly:
StringTokenizer st = new StringTokenizer(stringToSplit, "?");
while (st.hasMoreTokens()) {
// Use tokens
String token = st.nextToken();
}
Easiest way is to quote or backslash them:
<string name="random">"?"</string>
<string name="random">\?</string>
The final code.
String startDelim = ("\\?");
String realDelim = (startDelim);
String[] parts4 = fIdStpOne.split(realDelim);
String fIdStpTwo= parts4[0];
String JUNK4 = parts4[1];
Normally you'd just put it literally, like
String q = "?";
However, you say you're using it to split a string. split() takes a regular expression and ? is a metacharacter in a regex. To escape it, add a backslash in front. Backslash is a special character in Java string literals so it needs to be escaped, too:
String q = "\\?";
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