Guys I want to add \ before double quote " in a string..
for example :-
String s = "FB "Party" event";
Output should be:-
String Output = "FB \"Party\" event"
I tried a lot but no luck.
Thanks
Try this "FB \\\"Party\\\" event"
First two slashes is one slash and \" is double quote
Use a simple technique..
use escape character. Like for your case it would be something like
String input = "FB \\\"Party\\\" event";
System.out.println(input);
Related
if String s="\t\n";
then I want to print s and result will be : "\t\n" .
Example:
String s = "\t\n";
System.out.println(s);
Output: \t\n
Is there any way to do this?
Simply use str.replace():
String s = "\n\t";
s = s.replace("\n", "\\n");
s = s.replace("\t", "\\t");
System.out.println(s);
Output: \n\t
you can use "\\n\\t" to do so.Since \n is the new line character and \t is the tab. But if you need to print \ you need to use \\
Can you guys help me??
I have a string here :
a$20=A.createVar("/LIST/S_UNB/C_S001/D_0001/*var", a$1, this);
Now I want to replace the string in "" with the value that appears after last '/'.
here I want result to be
a$20=A.createVar("*var", a$1, this);
I am trying to use as minimal objects as possible and my regex looks like this
\"([^\"]*)\"
Is this correct?
Assuming the quotes aren't part of the expression, use
[^/]+$
$ signifies the end of the string, which will make it return only the value after the last '/'.
You can use this code:
String s = "a$20=A.createVar(\"/LIST/S_UNB/C_S001/D_0001/*var\", a$1, this);";
// extract text between ""
String sub = s.replaceAll("^[^\"]*\"([^\"]*)\".*$", "$1");
// find last index of /
int i = sub.lastIndexOf('/');
// replace content between "" by token after last /
String repl = s.replaceFirst("\"[^\"]*\"", '"' + sub.substring(i+1) + '"');
//=> a$20=A.createVar("*var", a$1, this);
What I have is a string array that I am creating from a .csv file I am reading. I then want to parse the values I'm going to use for the ' character and replace it with a \' because I am outputting this to a javascript file.
Here's the code I'm using for that:
while ((thisLine = myInput.readLine()) != null) {
String[] line = thisLine.split("\t");
if(line[4].indexOf("'") > -1){
System.out.println(line[4]);
line[4] = line[4].replace("'", "\'");
System.out.println(line[4]);
}
brand.add(line[4]);
}
However this is not working. I am getting the same string back after I do the replace.
Is this because of some issue with the string array?
I appreciate any assistance in this matter.
Try like this:
line[4] = line[4].replace("'", "\\'");
The backslash must be "escaped".
In case of line[4] = line[4].replace("'", "\'"); the part \' is converted to just '
You're falling foul of the fact that "'" is the same as "\'". They're the same string (a single character, just an apostrophe) - the escaping is there to allow a character literal of '\''.
You want:
line[4] = line[4].replace("'", "\\'");
So now you're escaping the backslash, instead of the apostrophe. So you're replacing apostrophe with backslash-then-apostrophe, which is what you wanted.
See JLS section 3.10.6 for details of escaping in character and string literals.
you should add back slash \ something like this
line[4] = line[4].replace("'", "\\'");
because one left slash \ is escape character
Your issue looks like it is an escape issue. Try \\ to replace a single back slash.
I have a string like "C:\Program Files\Directory\Something.txt" and I would like to convert it into "C:\\Program Files\\Directory\\Something.txt" So basically add \ wherevever a \ is found. Is the best way to approach this using indexOf and breaking the string into sustrings and then concatenating again or is there a more efficent way of doing this in Java?
String s = "C:/Program Files/Directory/Something.txt";
String s2 = s.replaceAll("/", "//");
// => "C://Program Files//Directory//Something.txt"
[Edit]
If the string has backslashes then it gets really fun since that is the regular expression escape character. This should work:
String s = "C:\\Program Files\\Directory\\Something.txt";
s.replaceAll("\\\\", "\\\\\\\\");
// => "C:\\\\Program Files\\\\Directory\\\\Something.txt"
Note that's four backslashes in the regex (two pairs in sequence to get two literal backslashses) and then eight in the replacement string since backslashes are also escape characters for literal strings.
You could use the File.separator property to make it more cross-platform:
String input = "C:/Program Files/Directory/Something.txt";
String result = input.replaceAll(File.separator, File.separator + File.separator);
You can just use String.replaceAll for this.
String str = "C:/Program Files/Directory/Something.txt";
str = str.replaceAll("/","//");
String s = "C:/Program Files/Directory/Something.txt";
s = s.replace("/", "//");
This will replace all / in the string with //
I have
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\\";
How can i replace
" to \"
and \ to \\
?
String result = a.replace("\"", "\\\"");
OR
String result = a.replace(""", "\"");
String result = a.replace("\\","\\\\").replace("\"", "\\\"");
This would first replace all \ with \\ and then all " with \" if that is what you want.
Note that doing it the other way round would result in " being replaced with \\" in the end, since first it get replaced with \" and then the \ would be replaced with \\ resulting in \\".
Additional note: your data string is not well-formed and should not compile: it ends in \" which is not a valid string literal delimiter (the literal ends in \\\\\" which would be the string data \\") - change that to an even number of slashes or add another " to the end in order to fix that.
The former. The latter is not well-formed Java code.
Since "String result = a.replace(""", "\"");" does not compile, does that answer your question?
you a string error,Less a quote
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\"";
System.out.println(a.replace("\"", "\\\""));