Replace string containing " with \"? - java

How can I replace string containing " with \" ?
replace(""","\"") is not working for me.
public static String replaceSpecialCharsForJson(String str){
return str.replace("'","\'")
.replace(":","\\:")
.replace("\"","\"")
.replace("\r", "\\r")
.replace("\n", "\\n");
}

You can try with:
replace("\"","\\\"")
Since both " and \ are metacharacters, you have to escape them with \

Try this one:
replace("\"","\\\"");

Every slash as part of a string needs to be escaped. So if you want a string to look like "\\", your code will have to contain String s = "\\\\". Ugly but true.
The same goes for any other special character that might be interpreted. Quotes and colons inclusive.
This means " \ " " will look like " \\ \" " (Added spaces to make separate escapes more visible)

Use:
str.replace("\"","\\\"")
So you escape the backslash.

You want to
replace " (properly escaped: \")
with \" (properly escaped: \\\").
The right call is:
replace("\"", "\\\"");

I tried this way. I don't know how much this helpful in your scenario
String oldStr = String.valueOf('"');
String newStr = File.separator.concat(String.valueOf('"'));
System.out.println(oldStr.replace(String.valueOf('"'),newStr));

Related

How to replace single quote within a string with two single quotes in java?

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.

Java pattern regex with escape characters

I want to replace ";" with "\n" except when it's escaped with a leading '\'. I haven't figured out the correct regex.
Here is what I have:
String s = "abc;efg\\;hij;pqr;xyz\\;123"
s.replaceAll("\\[^\\\\];", "\\\\n");
I'd expect the above string to be replaced with "abc\nefg\;hij;pqr;xyz\;123"
Use a negative look behind:
s = s.replaceAll("(?<!\\\\);", "\n");
The expression (?<!\\) (coded as a java string literal "(?<!\\\\)") means "the previous character should not be a backslash"
Test code:
String s = "abc;efg\\;hij;pqr;xyz\\;123";
s = s.replaceAll("(?<!\\\\);", "\n");
System.out.println(s);
Output:
abc
efg\;hij
pqr
xyz\;123

Split string by array of characters

i want to split a string by array of characters,
so i have this code:
String target = "hello,any|body here?";
char[] delim = {'|',',',' '};
String regex = "(" + new String(delim).replaceAll("(.)", "\\\\$1|").replaceAll("\\|$", ")");
String[] result = target.split(regex);
everything works fine except when i want to add a character like 'Q' to delim[] array,
it throws exception :
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 11
(\ |\,|\||\Q)
so how can i fix that to work with non-special characters as well?
thanks in advance
how can i fix that to work with non-special characters as well
Put square brackets around your characters, instead of escaping them. Make sure that if ^ is included in your list of characters, you need to make sure it's not the first character, or escape it separately if it's the only character on the list.
Dashes also need special treatment - they need to go at the beginning or at the end of the regex.
String delimStr = String(delim);
String regex;
if (delimStr.equals("^") {
regex = "\\^"
} else if (delimStr.charAt(0) == '^') {
// This assumes that all characters are distinct.
// You may need a stricter check to make this work in general case.
regex = "[" + delimStr.charAt(1) + delimStr + "]";
} else {
regex = "[" + delimStr + "]";
}
Using Pattern.quote and putting it in square brackets seems to work:
String regex = "[" + Pattern.quote(new String(delim)) + "]";
Tested with possible problem characters.
Q is not a control character in a regex, so you do not have to put the \\ before it (it only serves to mark that you must interpret the following character as a literal, and not as a control character).
Example
`\\.` in a regex means "a dot"
`.` in a regex means "any character"
\\Q fails because Q is not special character in a regex, so it does not need to be quoted.
I would make delim a String array and add the quotes to these values that need it.
delim = {"\\|", ..... "Q"};

How to replace \ with . in java String

I want to replace \ with . in String java.
Example src\main\java\com\myapp\AppJobExecutionListener
Here I want to get like src.main.java.com.myapp.AppJobExecutionListener
I tried str.replaceAll("\\","[.]") and str.replaceAll("\\","[.]") but it is not working.
I am still getting original string src\main\java\com\myapp\AppJobExecutionListener
String is immutable in Java, so whatever methods you invoke on the String object are not reflected on it unless you reassign it.
String s = "ABC";
s.replaceAll("B","D");
System.out.println(s); //still prints "ABC"
s = s.replaceAll("B","D");
System.out.println(s); //prints "ADC"
Currently you're using replaceAll, which takes regular expression patterns. That makes life much more complicated than it needs to be. Unless you're trying to use regular expressions, just use String.replace instead.
In fact, as you're only replacing one character with another, you can just use character literals:
String replaced = original.replace('\\', '.');
The \ is doubled as it's the escape character in Java character literals - but as the above doesn't use regular expressions, the period has no special meaning.
Assign it back to string str variable, .String#replaceAll doesn't changes the string itself, it returns a new String.
str = str.replaceAll("\\\\",".")
Can you try this:
String original = "Some text with \\ and rest of the text";
String replaced = original.replace("\\",".");
System.out.println(replaced);
'\' character is doubled in a string like '\\'. So '\\' character should be used to replace it with '.' character and also using replace instead of replaceAll would be enough to make it. Here is a sample;
public static void main(String[] args) {
String myString = "src\\main\\java\\com\\vxl\\appanalytix\\AppJobExecutionListener";
System.out.println("Before Replaced: " + myString);
myString = myString.replace("\\", ".");
System.out.println("After Replaced: " + myString);
}
This will give you:
Before Replaced: src\main\java\com\vxl\appanalytix\AppJobExecutionListener
After Replaced: src.main.java.com.vxl.appanalytix.AppJobExecutionListener
With String replaceAll(String regex, String replacement):
str = str.replaceAll("\\\\", ".");
With String replace(char oldChar, char newChar):
str = str.replace('\\', '.');
With String replace(CharSequence target, CharSequence replacement)
str = str.replace("\\", ".");
String replaced = original.replace('\', '.');
try this its works well
Use replace instead of replaceall
String my_str="src\\main\\java\\com\\vxl\\appanalytix\\AppJobExecutionListener";
String my_new_str = my_str.replace("\\", ".");
System.out.println(my_new_str);
DEMO AT IDEONE.COM
replaceAll takes a regex as the first parameter.
To replace the \ you need to double escape. You need an additional \ to escape the first . And as it is a regex input you need to escape those again. As other answers have said string is immutable so you will need to assign the result
String newStr = str.replaceAll("\\\\", ".");
The second parameter is not regex so you can just put . in there but note you need four slashes to replace one backslash if using replaceAll
i tried this:
String s="src\\main\\java\\com\\vxl\\appanalytix\\AppJobExecutionListener";
s = s.replace("\\", ".");
System.out.println("s: "+ s);
output: src.main.java.com.vxl.appanalytix.AppJobExecutionListener
Just change the line to
str = str.replaceAll("\\",".");
Edit : I didnt try it, because the problem here is not whether its a correct regex,but the problem here is that he is not assigning the str to new str value. Anyways regex corrected now.

replace in java using regular expression

suppose I have a string
String = ".... helllo.... good \"morning\" .....\" "
I want to get output as
helllo good morning
How can I do that using regular expression in Java?
If you're just trying to remove the . and the ", then you can do
str = str.replaceAll("\"|\\.", "");
This regular expression replaces any " (escaped as \" because in a java string literal) or (|) . (escaped first as \. because in a regex then as \\. because a \ must be escaped in a java string literal) by nothing ("").
This
String yourString = ".... helllo.... good \"morning\" .....\" ";
System.out.println(yourString.replaceAll("[.\\\"]", ""));
outputs helllo good morning
Supposing you just want to maintain the space character and letters, you can use the following regex:
[^a-zA-Z\s]+
If you also want to include numbers:
[^a-zA-Z0-9\s]+
Just replace the matches of that regular expression by an empty string.
Edit:
If you just want to do the opposite (remove certain characters, like . and "), then you can check #dystroy answer.
public static void main(String[] args) {
String str = ".... helllo.... good \"morning\" .....\" ";
str = str.replaceAll("[^a-zA-Z]", " ").replaceAll(" +", " ");
System.out.println(str);
}
There are many ways to do this.
Here is a way to do it using simple replace methods of String class.
String s = ".... helllo.... good \"morning\" .....\" ";
s = s.replace(".","").replace("\"", "");
System.out.println(s);

Categories

Resources