how to replace a char within a string with "nothing" [duplicate] - java

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.

Related

How can I replace a char sequence to "\n" [duplicate]

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

Remove all occurrences of char "." from string in Java [duplicate]

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 4 years ago.
I would like to remove the period/decimal character from a String using Java.
String originalString = "1.2345";
originalString = originalString.replaceAll(".", "");
Printing originalString returns empty.
How can I remove . from originalString?
The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:
originalString = originalString.replaceAll("\\.", "");
String originalString = "1.2345";
originalString = originalString.replaceAll("\\.", "");

Replace Last Occurrence of a character in a string Using Java [duplicate]

This question already has answers here:
Replace the last part of a string
(11 answers)
Closed 5 years ago.
I am having a string like this
String s1 = "2999.1049.00_GRB.1";
String s2 = "my File.txt.txt";
I want to replace the last ".1" with "_1" and ".txt" with "_txt"
The result of the String should be
s1 = "2999.1049.00_GRB_1" and s2 = "my File.txt_txt"
How can I do this. I am aware of replacing the first occurrence of the string. but don't know how to replace the last occurrence of a string.
Simply use .replace with lastIndexOf method of string
System.out.println(s.replace(s.substring(s.lastIndexOf(".1"), s.length()), "_1"));
You can use regex :
s = s.replaceAll("(.*)\\.(\\d+)$","$1_$2");
// (everything)point(digits) -> (everything)underscore(digits)
It will capture all element before the . in a group (group1), the digit(s) after in another group (group2), and replace by : group1_group2
the first group can be whatever you want
the second group is just digits, even more than 1
Regex demo

Using regex to trim a specific word in String [duplicate]

This question already has answers here:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.

Replace String ignoring case in Java [duplicate]

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, "");

Categories