This question already has answers here:
Java String.replace/replaceAll not working
(4 answers)
Closed 2 years ago.
I want to replace the string "pqtd" to "\n", and my code is:
String str = "this is my pqtd string";
if (str.contains("pqtd")) {
str.replaceAll("pqtd", "\n");
}
But that doesn't go, if I change all the code, doing it in reverse (trying to replace "\n" to "pqtd") it goes, so I think the problem is that Java can not replace a char sequence to "\n", at least I don't know-how.
There are multiple problems:
You check if your String contains "pqtd" but then try to replace "dtdpq" which doesn't appear anywhere in your String. I'm really not sure where that extra "d" and "q" are coming from.
You are using the methode replaceAll which takes a regular expression as first argument. Since yu want to replace a literal String you don't need to use regular expressions and can just use the standard replace method.
String are immutable and cannot be modified. Therefor all replace options will not modify the original String but instead return the modified String as a return value. You need to use that return value and assign your String to it if you wanr any changes in your String to happen at all.
Fixing all these 3 problems:
String str = "this is my pqtd string";
if (str.contains("pqtd")) {
str = str.replace("pqtd", "\n");
}
System.out.println(str);
Which will produce the expected output of
this is my
string
Related
This question already has answers here:
Java replace method, replacing with empty character [duplicate]
(2 answers)
Closed 2 years ago.
String mine = sc.next();
String corrected = mine.replace('.', '????');
System.out.println(corrected);
that's my code. let's assume that my input on String corrected is "<..><.<..>>" , and I want to replace every "." with a null space, so I get an output like "<><<>>". is there any way to do it?
If you want to replace . with an empty ("") string, you can just do:
mine.replace(".", "");
Alternatively, you can also check .replaceAll()
Try this to replace all occurrences of . with empty:
mine.replaceAll("\\.", "")
If you don't want any method, you can do it like this.
String str = "<<.>>.<>.<<.";
String [] parts = str.split("\\.");
for(String s:parts){
System.out.print(s);
}
Because I tried the method replaceAll(".", "") ;
But that method does not allow empty or null spaces in a string.
I don't know if it's the best way, but that's what I can think of.
Hello all I an new to java I just want to know that can we convert "Hello" in Hello. I have gone through the internet answers but found that if any string has "" in that so we can use the replace method of java. So I just want to convert the "Hello" into Hello. So if you know please help
suppose
String s="Hello"
//Required Operation
System.out.println(s);//It should print Hello.
So if you know please help me. Actually I have a file which contains lots of data having " " and I only want that data without double quotes so is it possible to convert that.
Here is an example:
String s = "\"hello\"";
String result = s.replaceAll("\"", "");
System.out.println(result);
Actually if you declare your string String s="Hello", the variable s will not contain any quotes, because the quotes are Java syntax and mark the start and end of the String.
Use String.replaceAll()
str = str.replaceAll("\"", "");
As all the other answers you're able to use:
str = str.replaceAll("\"", "");
But their is another solution if you just want to erase the 1st and last char of your string ( so your " here) is to use substring like:
str = "Hello";
str = str.substring(0,str.length()-2);
I think that it could work for you
This question already has answers here:
How to replace case-insensitive literal substrings in Java
(10 answers)
Closed 7 years ago.
String Checkout = D:\ifs\APP\Checkout
String DeleteLine = D:\IFS\APP\Checkout\trvexp\client\Ifs.App\text.txt
Note the ifs and IFS in both Strings.
I want to replace the Checkout String in the Deleted Line
So the final String would look like this:
\trvexp\client\Ifs.App\text.txt
Following is what I have tried, but obviously due to Case Sensitivity, the string won't get replaced. Any Solution or a work around for this?
String final = DeleteLine.replace(Checkout, "");
String.replace() doesn't support regex. You need String.replaceAll().
DeleteLine.replaceAll("(?i)" + Pattern.quote(Checkout), "");
Put (?i) in the replaceAll method's regular expression:
String finalString = DeleteLine.replaceAll("(?i)" + Checkout, "");
You can do this:
String Checkout = "D:\\\\ifs\\\\APP\\\\Checkout";
String DeleteLine = "D:\\IFS\\APP\\Checkout\\trvexp\\client\\Ifs.App\\text.txt";
String f = DeleteLine.replaceFirst("(?i)"+Checkout, "");
Alternatively, if youi want the pattern on a specific portion you can do it manually. You can declare the checkout Sting as:
String Checkout= \Q(?i)D:\ifs\APP\Checkout\E
as
\Q means "start of literal text"
\E means"end of literal text"
and then do the replace
String final = DeleteLine.replace(Checkout, "");
So, I'm trying to parse a String input in Java that contains (opening) square brackets. I have str.replace("\\[", ""), but this does absolutely nothing. I've tried replaceAll also, with more than one different regex, but the output is always unchanged. Part of me wonders if this is possibly caused by the fact that all my back-slash characters appear as yen symbols (ever since I added Japanese to my languages), but it's been that way for over a year and hasn't caused me any issues like this before.
Any idea what I might be doing wrong here?
Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:
str = str.replaceAll("\\[", "");
For the normal replace method, you don't need to escape the bracket:
str = str.replace("[", "");
public String replaceAll(String regex, String replacement)
As shown in the code above, replaceAll method expects first argument as regular expression and hence you need to escape characters like "(", ")" etc (with "\") if these exists in your replacement text which is to be replaced out of the string. For example :
String oldString = "This is (stringTobeReplaced) with brackets.";
String newString = oldString.replaceAll("\\(stringTobeReplaced\\)", "");
System.out.println(newString); // will output "This is with brackets."
Another way of doing this is to use Pattern.quote("str") :
String newString = oldString.replaceAll(Pattern.quote("(stringTobeReplaced)"), "");
This will consider the string as literal to be replaced.
As always, the problem is not that "xxx doesn't work", it is that you don't know how to use it.
First things first:
a String is immutable; if you read the javadoc of .replace() and .replaceAll(), you will see that both specify that a new String instance is returned;
replace() accepts a string literal as its first argument, not a regex literal.
Which means that you probably meant to do:
str = str.replace("[", "");
If you only ever do:
str.replace("[", "");
then the new instance will be created but you ignore it...
In addition, and this is a common trap with String (the other being that .matches() is misnamed), in spite of their respective names, .replace() does replace all occurrences of its first argument with its second argument; the only difference is that .replaceAll() accepts a regex as a first argument, and a "regex aware" expression as its second argument; for more details, see the javadoc of Matcher's .replaceAll().
For it to work it has to be inside a method.
for example:
public class AnyClass {
String str = "gtrg4\r\n" + "grtgy\r\n" + "grtht\r\n" + "htrjt\r\n" + "jtyjr\r\n" + "kytht";
public String getStringModified() {
str.replaceAll("\r\n", "");
return str;
}
}
This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 6 years ago.
I want to check if my string contains a + character.I tried following code
s= "ddjdjdj+kfkfkf";
if(s.contains ("\\+"){
String parts[] = s.split("\\+);
s= parts[0]; // i want to strip part after +
}
but it doesnot give expected result.Any idea?
You need this instead:
if(s.contains("+"))
contains() method of String class does not take regular expression as a parameter, it takes normal text.
EDIT:
String s = "ddjdjdj+kfkfkf";
if(s.contains("+"))
{
String parts[] = s.split("\\+");
System.out.print(parts[0]);
}
OUTPUT:
ddjdjdj
Why not just:
int plusIndex = s.indexOf("+");
if (plusIndex != -1) {
String before = s.substring(0, plusIndex);
// Use before
}
It's not really clear why your original version didn't work, but then you didn't say what actually happened. If you want to split not using regular expressions, I'd personally use Guava:
Iterable<String> bits = Splitter.on('+').split(s);
String firstPart = Iterables.getFirst(bits, "");
If you're going to use split (either the built-in version or Guava) you don't need to check whether it contains + first - if it doesn't there'll only be one result anyway. Obviously there's a question of efficiency, but it's simpler code:
// Calling split unconditionally
String[] parts = s.split("\\+");
s = parts[0];
Note that writing String[] parts is preferred over String parts[] - it's much more idiomatic Java code.
[+]is simpler
String s = "ddjdjdj+kfkfkf";
if(s.contains ("+"))
{
String parts[] = s.split("[+]");
s = parts[0]; // i want to strip part after +
}
System.out.println(s);