I have this string "Distrik Timur". I want to remove " from this string.
I thought String value = translatedValue.replace(""","") will work.But its not working.
Quotation marks need to be escaped: write translatedValue.replace("\"", "").
Related
I have the below data in my text file.
1|"John"|3,5400
2|"Jim"|7,7300
3|"Smith,Robin",3,4300
4|"O'Brien",10,8200
and I want this output:
(1,'John',3,5400)
(2,'Jim',7,7300)
(3,'Smith,Robin',3,4300)
(4,'O''Brien',10,8200)
Basically I want to replace | character with commas and double quotes with single quote. I am able to achieve that with this piece of code:
String text2 = textAfterHeader.replaceAll("\\|", ",").replaceAll("\"", "'").replaceAll("[a-zA-Z]'[a-zA-Z]", "''");
output that I am getting:
1,'John',3,5400
2,'Jim',7,7300
3,'Smith,Robin',3,4300
4,'''rien',10,8200
But I have one more requirement where I need to put two single quotes whenever a single quote appears between a string, for example, O'Brien as O''Brien. But this part is not working.
As was suggested by #AndyTurner, you can simplify the problem by first replacing all ' with '' and then replace all " with '. The only thing missing after that are the parenthesis, which can be added in two steps:
Replace all blanks with ) ( (notice the blank between the parenthesis).
Add a leading ( and a trailing ) to the String.
All together, a solution could look like this:
final String output = "("
+ input
.replace("'", "''")
.replace("\"", "'")
.replace("|", ",")
.replace(" ", ") (")
+ ")";
Ideone demo
Try this regex:
\s*\'\s*
and a call to Replace with " will do the job.
A possible solution could be:
String lineSeparator = System.getProperty("line.separator");
String output = new StringBuilder("(").append(
input.replaceAll("\\|", ",")
.replaceAll("'", "''") // (*)
.replaceAll("\"", "'") // (**)
.replaceAll(lineSeparator, ")" + lineSeparator + "("))
.append(")").toString();
Note the replacement (*) must come before (**). Since you need to replace exact characters instead of variable regular expressions, you want better use replace instead of replaceAll.
In an Android Java project, I have a string like this one (although with varying amounts of whitespace on either side):
String foo = " foo bar "
The whitespace on the two sides of the string is important, as the actual string contains indented code with HTML syntax highlighting.
When I pass the string through Html.fromHtml, this start and end whitespace is removed, but I need to keep the whitespace there:
Html.fromHtml(foo).toString() // "foo bar" - I want " foo bar "
How I can preserve the whitespace on the sides of the string through the Html.fromHtml call?
Try Using TextUtils.htmlEncode(str).
This method will escape all html string character.
https://developer.android.com/reference/android/text/TextUtils.html#htmlEncode(java.lang.String)
Yazan's suggestion was sufficient,but since you said that the string is generated dynamically,you can always take the newly generated string s for example and use the concat() method.For example s.concat("  ");
As Html.fromHtml() parses the string as html tags and content may be you want to use the encoded character for space which is
try this in your code
String foo = " foo bar ";
Note: repeat as many spaces as you need to show.
Edit:
if you are getting your string from somewhere else, you can replace spaces with before passing it
String foo = getMyFoo();
foo = foo.replaceAll(" "," ");
For preserving starting whitespace, this Kotlin code appears to work, and probably wouldn't be difficult to adapt for working with ending whitespace either:
fun replaceWithNonBreakingAtStart(str: String) = (1..(str.takeWhile { it == ' ' }.count())).map { " " }.joinToString("") + str.trimStart()
I found the question here before but somehow I can't see what I'm doing wrong. So I have a given String that looks something like this:
"Some text here\n\nsome more text here"
And I want to remove the linebreaks and display the Text in a TextView. I tried using String.replaceAll:
String newString = oldString.replaceAll("\\n", " ");
But that didn't change anything in the text. However,
oldString.contains("\\n"); returns true. What am I doing wrong?
edit:
I'm sorry, I know, oldString doesn't change. The problem is that, if I print oldString and newString they're exactly the same even though it says that oldString does contain a "\n".
This is my code:
Log.d(TAG, "contains: " + str.contains("\\n"));
Log.d(TAG, "old: " + str);
str = str.replaceAll("\\n", " ");
Log.d(TAG, "new: " + str);
And this is what I get:
contains: true
old: Vorgang nicht möglich\n\nBitte Karte entnehmen
new: Vorgang nicht möglich\n\nBitte Karte entnehmen
UPDATE
Thanks to Shivanshu Verma, I tried str.replace("\\n"" "); instead of str.replaceAll("\\n", " "); and that works! Does anybody know, why I can't use replaceAll() here?
Strings in Java are immutable and as such the new string with the replacements is stored in newString, not oldString.
EDIT
I see now that your issue was not actually related to Java String immutability but rather the difference between replace() and replaceAll(). The difference between these is that replaceAll() takes in a regex as the first argument, which will then replace any matches with the second argument, whereas replace() simply takes in a CharSequence (of which String is an implementation) and will replace exact matches with the second argument.
In your case, I think your original String had the newline characters escaped:
String str = "Vorgang nicht möglich\\n\\nBitte Karte entnehmen";
which meant that the String didn't actually contain newline characters at all; it contained literally "\n". This would mean that:
str.replaceAll("\\n", " ");
will resolve the first argument to a regex and replace newline characters (of which there were none), and:
str.replace("\\n", " ");
will replace exact matches of "\n". It's also worth noting that as others have pointed out contains() also doesn't take in a regex, which is why running:
oldString.contains("\\n");
returned true.
Your code works perfectly fine.
This test will pass without any error:
#Test
public void testReplaceAll() {
String newString = "line1\nline2\nline3".replaceAll("\\n", " ");
assertThat(newString).isEqualTo("line1 line2 line3");
assertThat(newString).doesNotContain("\\n");
}
try to Replace() method instead of ReplaceAll()
String newString = oldString.replace("\\n", " ");
may be it work for u
String newString = oldString.replace("\n", " ");
In Java, I want to print a label with a String as the input:
String command
= "N\n"
+ "A50,5,0,1,2,2,N,\"" + name + "\"\
+ "P1\n";
But when the input (name) has a double quote character ("), it is blank and prints nothing. I have tried using the replace function:
name.replace('"', '\u0022');
but it doesn't work. I want that double quote printed in label, how can I do this?
Sending the " character in the text field of the EPL string makes the EPL code think it is the end of the string you are trying to print.
So, if you want to send(and print) "hello" you have to put a backslash before each " character and send \"hello\"
You also have to do that for backslashes.
So, your (EPL)output to the printer would have quotes to begin and end the string, and \" to print the quote characters WITHIN the string :
A30,210,0,4,1,1,N,"\"hello\""\n
Also remember you have to escape to characters to build a c# string so in c# it would look like this:
outputEPLStr += "A30,210,0,4,1,1,N,\"\\"hello\\"\"\n";
[which contains 6 escaped characters]
Couple of points:
replace method returns back string after replacing so you should expect something like:
command = command.replace...
quote has special meaning and hence needs to be escaped in Java. You need the following:
name = name.replace("\"", "");
String command
= "N\n"
+ "A50,5,0,1,2,2,N,\"" + name + "\""
+ "P1\n";
System.out.println(command);
I have following question. In my app there is a listview. I get itemname from listview and transfer it to the webview as a string. How to ignore case of this string and change spaces to underscores?
For example: String itemname = "First Topic". I transfer it to the next activity and want to ignore case and change space to underscore (I want to get first_topic in result).
I get "itemname" in webviewactivity and want to do what I've described for following code:
String filename = bundle.getString("itemname") + ".html";
Please, help.
use replaceAll and toLowerCase methods like this:
myString = myString.replaceAll(" ", "_").toLowerCase()
This works for me:
itemname = itemname.replaceAll("\\s+", "_").toLowerCase();
replaceAll("\\s+", "_") replaces consecutive whitespaces with a single underscore.
"first topic".replaceAll("\\s+", "_") -> first_topic
"first topic".replaceAll(" ", "_") -> first__topic
You can use the replaceAll & toLowerCase methods but keep in mind that they don't change the string (they just return a modified string) so you need to assign the back to the variable, eg.
String itemname = bundle.getString("itemname");
itemname = itemname.replaceAll(" ", "_").toLowerCase();
String filename = itemname + ".html";