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, "");
Related
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
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("\\.", "");
This question already has answers here:
Why can't I split a string with the dollar sign?
(6 answers)
Closed 7 years ago.
I have a IPv6 string
String str = "demo1 26:11:d0a2:f020:0:0:0:a3:2123 demo2";
String searchString = "26:11:d0a2:f020:0:0:0:a3:2123";
When i use str.split(searchString) code returns
["demo1 ", " demo2"]
Which is fine but when i use:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "[26:11:d0a2:f020:0:0:0:a3]:2123";
and do str.split(searchString) it reutrns
[demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2]
Which is wrong i guess , can some one tell why I am getting this sort of output?
Since split function takes a regex as parameter, you need to escape those brackets otherwise this [26:11:d0a2:f020:0:0:0:a3] would match a single character only.
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
str.split(searchString);
It is happening because split(String str) take regex pattern string as argument. And that string will be used as regex pattern to match all the delimiter with this pattern.
In your regex pattern you are providing character sets in [].
To make it work your way you will have to use this regex pattern string :
\[26:11:d0a2:f020:0:0:0:a3\]:2123
i.e. in java :
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
I hope you are familiar with the string regexs. In java, the regex [abc] means match with a OR b OR c I encourage you to escape your square brackets try:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
You have to use an escape sequence for some special characters. Use \\[ ... \\] in the searchString variable.
How to replace a sub-string in java of form " :[number]: "
example:
string="Hello:6:World"
After replacement,
HelloWorld
ss="hello:909:world";
do as below:
String value = ss.replaceAll("[:]*[0-9]*[:]*","");
You can use a regex to define your desired pattern
String pattern = "(:\d+:)";
string EXAMPLE_TEST = ':12:'
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "text to replace with"));
should work depending on what exactly you want to replace...
Do like this
String s = ":6:";
s = s.replaceAll(":", "");
Edit 1: After the question was changed, one should use
:\d+:
and within Java
:\\d+:
This is the answer for replacing :: as well.
This is the regexp you should use:
:\d*:
Debuggex Demo
And here is a running JavaCode snipped:
String str = "Hello :4: World";
String s = str.replaceAll(":\\d*:","");
System.out.println(s);
One problem with replaceAll is often, that the corrected String is returned. The string object from which replaceAll was called is not modified.
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);