Replace all "?" with "\\?" in Java [duplicate] - java

This question already has answers here:
Replace a question mark (?) with (\\?)
(2 answers)
replace String with another in java
(7 answers)
Closed 9 years ago.
is it possible to replace all the questionmarks ("?") with "\?" ?
Lets say I have a String, and I want to delete some parts of that String, one part with an URL in it. Like this:
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring.replaceAll(replacestring, "");
But! As I understand it you can't use the replaceAll() method with a String that contains one single questionmark, you have to make them like this "\?" first.
So the question is; Is there some way to replace questionmarks with "\?" in a String? And no, I'm able to just change the String.
Thanks in advance, hope someone understands me! (Sorry for bad English...)

Don't use replaceAll(), use replace()!
It is a common misconception that replaceAll() replaces all occurrences and replace() just replaces one or something. This is totally incorrect.
replaceAll() is poorly named - it actually replaces a regex.
replace() replaces simple Strings, which is what you want.
Both methods replace all occurrences of the target.
Just do this:
longstring = longstring.replace(replacestring, "");
And it will all work.

Escape the \ too, using \\\\?.
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring=longstring.replaceAll(replacestring, "\\\\?");
But as other answer have mentioned, replaceAll is a bit overkill, just a replace should work.

replaceAll takes a regular expression, and ? has a special meaning in the regex world.
You should use replace in this case, since you don't need a regex.
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring = longstring.replace(replacestring, "");
Oh, and strings are immutable!! longstring = longstring.replace(..), notice the assignment.

Use String.replace() instead of String.replaceAll():
longstring = longstring.replace("?", "\\?");
String.replaceAll() uses Regular Expression, while String.replace() uses plain text.

Related

How I can fix it this "replaceAll" error? [duplicate]

This question already has answers here:
Java Regex matching between curly braces
(5 answers)
Closed 3 years ago.
How I can fix it?
String replace1 = WEBSITE.replaceAll("{fromNumber}", number);
this character "{" error in replaceAll function. Thank you
As #Stephen C has already explained replaceall method's first argument is a regex.
Looks you are trying to replace {fromNumber} simple string with a given number. So instead of using replaceall use replace method which accepts a string as a first argument.
String replace1 = WEBSITE.replace("{fromNumber}", number);
I is not working because '{' is a regex meta-character and replaceAll is using it as so. If you want to replace all "{fromNumber}" from you String then you have to :
String replace1 = WEBSITE.replaceAll("\{fromNumber\}", number);
But if you just have to replace one then you can go with #lahiruk's answer and use
String replace1 = WEBSITE.replace("{fromNumber}", number);
Something to add here , you can use replace any number of times if you know how many times your String will contain the String to be replaced.
For more info
Syntax of regexp.
String.repaceAll()

How to split String without regex [duplicate]

This question already has answers here:
String.split() *not* on regular expression?
(8 answers)
Closed 2 years ago.
In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.
Finding indices (by indexOf and lastIndexOf) does not use regex, so
String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3
gives me the index I need.
However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...
String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need either of the following:
A way to split into an array by a non-regex target
A way to "convert" a non-regex target string into a regex string
Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?
You need to escape your "target" in order to use it as a regex.
Try
String[] someStringSplit = someString.split(Pattern.quote(target));
and let me know if that helps.
String::split do split without regex if the regex is:
a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
two-char String and the first char is the backslash and the second is
not the ascii digit or ascii letter.
Please see String::split() source code for details.
For escaped '.' target it is going to be split without regex.
You can try this one.
String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);
while (tokenValue.hasMoreTokens()) {
System.out.println(tokenValue.nextToken());
}

Split a string using a string as delimiter

Is it possible to split a string using a string as delimiter? If yes, how?
Example :
String myString = "hello_world<;>goodbye<;>foo";
myString.split("<;>");
//returns "hello_world", "goodbye" and "foo"
The example in your question works exactly, however this may be coincidental. Keep in mind that the String.split(...) method accepts a RegEx parameter, not a String delimiter.
Check out the RegEx documentation here: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
yes, exactly as you have in your code.

How to replace a substring of a string [duplicate]

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 6 years ago.
Assuming I have a String string like this:
"abcd=0; efgh=1"
and I want to replace "abcd" by "dddd". I have tried to do such thing:
string.replaceAll("abcd","dddd");
It does not work. Any suggestions?
EDIT:
To be more specific, I am working in Java and I am trying to parse the HTML document, concretely the content between <script> tags. I have already found a way how to parse this content into a string:
if(tag instanceof ScriptTag){
if(((ScriptTag) tag).getStringText().contains("DataVideo")){
String tagText = ((ScriptTag)tag).getStringText();
}
}
Now I have to find a way how to replace one substring by another one.
You need to use return value of replaceAll() method. replaceAll() does not replace the characters in the current string, it returns a new string with replacement.
String objects are immutable, their values cannot be changed after they are created.
You may use replace() instead of replaceAll() if you don't need regex.
String str = "abcd=0; efgh=1";
String replacedStr = str.replaceAll("abcd", "dddd");
System.out.println(str);
System.out.println(replacedStr);
outputs
abcd=0; efgh=1
dddd=0; efgh=1
2 things you should note:
Strings in Java are immutable to so you need to store return value of thereplace method call in another String.
You don't really need a regex here, just a simple call to String#replace(String) will do the job.
So just use this code:
String replaced = string.replace("abcd", "dddd");
You need to create the variable to assign the new value to, like this:
String str = string.replaceAll("abcd","dddd");
By regex i think this is java, the method replaceAll() returns a new String with the substrings replaced, so try this:
String teste = "abcd=0; efgh=1";
String teste2 = teste.replaceAll("abcd", "dddd");
System.out.println(teste2);
Output:
dddd=0; efgh=1
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.
from javadoc.
You are probably not assigning it after doing the replacement or replacing the wrong thing.
Try :
String haystack = "abcd=0; efgh=1";
String result = haystack.replaceAll("abcd","dddd");

Replace all with a string having regex wild chars [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java String.replaceAll without regex
I have a string and I need to replace some parts of it.
The replacement text contains regex wild chars though. Example:
String target = "Something * to do in ('AAA', 'BBB')";
String replacement = "Hello";
String originalText = "ABCDEFHGIJKLMN" + target + "ABCDEFHGIJKLMN";
System.out.println(originalText.replaceAll(target, replacement));
I get:
ABCDEFHGIJKLMNSomething * to do in ('AAA', 'BBB')ABCDEFHGIJKLMN
Why doesn't the replacement occur?
Because *, ( and ) are all meta-characters in regular expressions. Hence all of them need to be escaped. It looks like Java has a convenient method for this:
java.util.regex.Pattern.quote(target)
However, the better option might be, to just not use the regex-using replaceAll function but simply replace. Then you do not need to escape anything.
String.replaceAll() takes a regular expression and so it's trying to expand these metacharacters.
One approach is to escape these chars (e.g. \*).
Another would be to do the replacement yourself by using String.indexOf() and finding the start of the contained string. indexOf() doesn't take a regexp but rather a normal string.

Categories