I need to escape quotes from a string before printing them out.
I've written a function, using char arrays to be as explicit as possible. But all this function seems to do is print out its own input:
public static String escapeQuotes(String myString) {
char[] quote=new char[]{'"'};
char[] escapedQuote=new char[]{'\\' , '"'};
return myString.replaceAll(new String(quote), new String(escapedQuote));
}
public static void main(String[] args) throws Exception {
System.out.println("asd\"");
System.out.println(escapeQuotes("asd\""));
}
I would expect the output of this to be:
asd"
asd\"
However what I get is:
asd"
asd"
Any ideas how to do this properly?
Thanks
I would try replace instead of replaceAll. The second version is designed for regex, AFAIK.
edit
From replaceAll docs
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string
So, backslash in second argument is being a problem.
The direct link to the docs
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
Java comes with readily available methods for escaping regexs and replacements:
public static String escapeQuotes(String myString) {
return myString.replaceAll(Pattern.quote("\""), Matcher.quoteReplacement("\\\""));
}
You also need to escape \ into \\ Otherwise any \ in your original string will remain \ and the parser, thinking that a special character (e.g. ") must follow a \ will get confused.
Every \ or " character you put into a Java literal string needs to be preceeded by \.
You want to use replace not replaceAll as the former deals with strings, the latter with regexps. Regexps will be slower in your case and will require even more backslashes.
replace which works with CharSequences i.e. Strings in this case exists from Java 1.5 onwards.
myString = myString.replace("\\", "\\\\");
myString = myString.replace("\"", "\\\"");
Related
import java.util.StringTokenizer;
class MySplit
{
public static void main(String S[])
{
String settings = "12312$12121";
StringTokenizer splitedArray = new StringTokenizer(settings,"$");
String splitedArray1[] = settings.split("$");
System.out.println(splitedArray1[0]);
while(splitedArray.hasMoreElements())
System.out.println(splitedArray.nextToken().toString());
}
}
In above example if i am splitting string using $, then it is not working fine and if i am splitting with other symbol then it is working fine.
Why it is, if it support only regex expression then why it is working fine for :, ,, ; etc symbols.
$ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the string "$", but as the special meta character $. One sexy solution is:
settings.split(Pattern.quote("$"))
Pattern#quote:
Returns a literal pattern String for the specified String.
... The other solution would be escaping $, by adding \\:
settings.split("\\$")
Important note: It's extremely important to check that you actually got element(s) in the resulted array.
When you do splitedArray1[0], you could get ArrayIndexOutOfBoundsException if there's no $ symbol. I would add:
if (splitedArray1.length == 0) {
// return or do whatever you want
// except accessing the array
}
If you take a look at the Java docs you could see that the split method take a regex as parameter, so you have to write a regular expression not a simple character.
In regex $ has a specific meaning, so you have to escape it this way:
settings.split("\\$");
The problem is that the split(String str) method expects str to be a valid regular expression. The characters you have mentioned are special characters in regular expression syntax and thus perform a special operation.
To make the regular expression engine take them literally, you would need to escape them like so:
.split("\\$")
Thus given this:
String str = "This is 1st string.$This is the second string";
for(String string : str.split("\\$"))
System.out.println(string);
You end up with this:
This is 1st string.
This is the second strin
Dollar symbol $ is a special character in Java regex. You have to escape it so as to get it working like this:
settings.split("\\$");
From the String.split docs:
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.
On a side note:
Have a look at the Pattern class which will give you an idea as to which all characters you need to escape.
Because $ is a special character used in Regular Expressions which indicate the beginning of an expression.
You should escape it using the escape sequence \$ and in case of Java it should be \$
Hope that helps.
Cheers
Let's consider the following code snippet in Java.
package escape;
final public class Main
{
public static void main(String[] args)
{
String s = "abc/xyz";
System.out.println(s.replaceAll("/", "\\\\"));
}
}
I just want to replace "/" with "\" in the above String abc/xyz and which is done and displays abc\xyz as expected but I couldn't get why it requires back slashes four times. It looks like two back slashes are sufficient. Why such is not a case?
The reason is that String.replaceAll uses regular expressions (and actually calls Matcher.replaceAll which does document this). In regular expressions you have to escape the '\' also in string literals you have to escape the '\'. Your 4 slashes are two slashes in the java string. And thereby an escaped slash in the regular expression.
You need to escape back slash once\\ for java String and one more time\\ for regex replacement string.
From the JavaDoc:
Note that backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special
meaning of these characters, if desired
System.out.println(s.replaceAll("/", Matcher.quoteReplacement("\\")));
I just want to replace "/" with "\"
Then you should not be using a regular expression, which is overkill, and requires backslashes to be escaped (twice). Instead do
string.replace('/', '\\');
(Still need to escape it once)
Referring to the Java documentation, if you use replaceAll, Java will treat the first parameter as a RegEx, and will assess special meaning for backslashes in the replacement string. Basically, \1 would refer to the first matching glob in the regex... In this case you need to escape the backslashes so they're "litteral" backslashes for the String, and then you need to escape these a second time so that replaceAll doesn't try to treat them with a special meaning.
Having a hard time replacing a quote with an escaped quote. I have a string that has the value of 'Foo "bar" foobar', and I am trying to replace the quotes around bar with escaped quotes, and it isn't working. I am going crazy.
s=s.replaceAll("\"","\\\"");
I would expect s to have the value of 'foo \"bar\" foobar', but it doesn't. Any help?
replaceAll uses regular expressions - in which the backslash just escapes the next character, even in the replacement.
Use replace instead and it's fine... or you can double the backslash in the second argument if you want to use the regular expression form:
String after = before.replaceAll("\"", "\\\\\"");
This could be useful if you need to match the input using regular expressions, but I'd strongly recommend using the non-regex form unless you actually need regex behaviour.
Personally I think it was a mistake for methods in String to use regular expressions to start with - things like foo.split(".") which will split on every character rather than on periods, completely unexpectedly to the unwary developer.
Use s = s.replace("\"", "\\\"");, which is the non-regex version.
You need to escape both replace all arguments because they are regex!
#Test
public void testReplace() {
assertEquals("foo\\\"bar\\\"",
"foo\"bar\"".replaceAll("\\\"", "\\\\\\\""));
}
So if you want to write " you need to escape the regex -> \" but because you are in java you need to escape the \ and " for java to, so you get \\\\". (that is the search parameter).
For the replace parameter you want to have \" -> in regex: \\\" -> in Java \\\\\\\"
s=s.replaceAll("\"","\\\\\"");
But you should really go with replace(...) as the others here advice.
Use;
s=s.replaceAll("\"","\\\\\"");
String str = "'Foo \"Bar\" Bar'";
System.out.println(str);
System.out.println(str.replaceAll("\"", "\\\\\""));
please follow below program and use > s.replace("/"."//");
public class stringbackslash {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="\";
s=s.replace("\", "\\");
System.out.println(s);
}
}
I am looking to replace \n with \\n but so far my regex attempts are not working (Really it is any \ by itself, \n just happens to be the use case I have in the data).
What I need is something along the lines of:
any-non-\ followed by \ followed by any-non-\
Ultimately I'll be passing the regex to java.lang.String.replaceAll so a regex formatted for that would be great, but I can probably translate another style regex into what I need.
For example I after this program to print out "true"...
public class Main
{
public static void main(String[] args)
{
final String original;
final String altered;
final String expected;
original = "hello\nworld";
expected = "hello\\nworld";
altered = original.replaceAll("([^\\\\])\\\\([^\\\\])", "$1\\\\$2");
System.out.println(altered.equals(expected));
}
}
using this does work:
altered = original.replaceAll("\\n", "\\\\n");
The string should be
"[^\\\\]\\\\[^\\\\]"
You have to quadruple backslashes in a String constant that's meant for a regex; if you only doubled them, they would be escaped for the String but not for the regex.
So the actual code would be
myString = myString.replaceAll("([^\\\\])\\\\([^\\\\])", "$1\\\\$2");
Note that in the replacement, a quadruple backslash is now interpreted as two backslashes rather than one, since the regex engine is not parsing it. Edit: Actually, the regex engine does parse it since it has to check for the backreferences.
Edit: The above was assuming that there was a literal \n in the input string, which is represented in a string literal as "\\n". Since it apparently has a newline instead (represented as "\n"), the correct substitution would be
myString = myString.replaceAll("\\n", "\\\\n");
This must be repeated for any other special characters (\t, \r, \0, \\, etc.). As above, the replacement string looks exactly like the regex string but isn't.
So whenever there is 1 backslash, you want 2, but if there is 2, 3 or 4... in a row, leave them alone?
you want to replace
(?<=[^\\])\\(?!\\+)([^\\])
with
\\$1
That changes the string
hello\nworld and hello\\nworld and hello\\\nworld
into
hello\\nworld and hello\\nworld and hello\\\nworld
I don't know exactly what you need it for, but you could have a look at StringEscapeUtils from Commons Lang. They have plenty of methods doing things like that, and if you don't find exactly what you're searching for, you could have a look at the source to find inspiration :)
Whats wrong with using altered = original.replaceAll("\\n", "\\\\n"); ? That's exactly what i would have done.
What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.
You need to escape the + for the regular expression, using \.
However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:
"\\+"
when in doubt, let java do the work for you:
myStr.replaceAll(Pattern.quote("+"), replaceStr);
You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.
So your regex string will be defined as "\\+" in Java code.
I.e. this example:
String test = "ABCD+EFGH";
test = test.replaceAll("\\+", "-");
System.out.println(test);
Others have already stated the correct method of:
Escaping the + as \\+
Using the Pattern.quote method which escapes all the regex meta-characters.
Another method that you can use is to put the + in a character class. Many of the regex meta characters (., *, + among many others) are treated literally in the character class.
So you can also do:
orgStr.replaceAll("[+]",replaceStr);
Ideone Link
If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:
mystr = StringUtils.replace(mystr, "+", "plus");
Say you want to replace - with \\\-, use:
text.replaceAll("-", "\\\\\\\\-");
String str="Hello+Hello";
str=str.replaceAll("\\+","-");
System.out.println(str);
OR
String str="Hello+Hello";
str=str.replace(Pattern.quote(str),"_");
System.out.println(str);
How about replacing multiple ‘+’ with an undefined amount of repeats?
Example: test+test+test+1234
(+) or [+] seem to pick on a single literal character but on repeats.