I have one variable representing the regex, when run replaceAll, none of string is replaced. Please help to take a look.
String s = "Issue 3 for 5 describe the title";
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
System.out.println(s.replaceAll(regex, "test"));
replaceAll returns the modified String, but it does not modify the original String, as String in Java is immutable.
You need to:
String resultString = s.replaceAll(regex, "test")
System.out.println(resultString);
Java String is immutable.
If you want to change string s use this:
s = s.replaceAll(regex, "test"));
This could be caused by the fact that in your code you have to double escape your regexp, but in the xml you don't:
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
is equivalent to the parsed
<regex>Issue\s\d+\sfor\s\d+</regex>
Related
I have to remove ## before $$ from string ##$$abxcyhshbhs##xcznbx##. I am using:
string.split("\\#");
The problem is that it also removes # after $$.
Use replace() instead.
String text = "##$$abxcyhshbhs##xcznbx##";
text = text.replace("##$$", "$$");
You can use substring method like below
string.substring(2);
If you really want to use String.split() you can do what you want by limiting the number of results by doing:
String str = "##$$abxcyhshbhs##xcznbx##";
str = str.split("##", 2)[1];
I don't know your exact issue but as has already been said, replace() or substring() is probably a better option.
If you have unknown number of # symbols before $$ and they appear not just at the beginning of the string, you can use the following replaceAll with a regex:
String re = "#+\\${2}";
String str = "##$$abxcyh###$$shbhs##xcznbx##";
System.out.println(str.replaceAll(re, "\\$\\$")); // Note escaped $ !!!
// => $$abxcyh$$shbhs##xcznbx##
// or
re = "#+(\\${2})"; // using capturing and back-references
System.out.println(str.replaceAll(re, "$1"));
See IDEONE demo.
Do not forget to assign the variable a new value when using in your code:
str = str.replaceAll("#+(\\${2})", "$1")
If your purpose is to remove ## from first occurrence of ##$$ in the string, then following code snippet will be helpful:
if(yourString.startsWith("##$$")){
yourString.replaceFirst("##$$","$$");
}
OR considering there is only single $$ in your string, following would be helpful:
String requiredString="";
String[] splitArr = yourString.split("\\$");
if ( splitArr.length > 1 ) {
requiredString = "$$" + splitArr[splitArr.length-1];
}
I have written a code snippet here. You can make changes and execute on your own.
To literally remove the first two characters, use the following:
String s = "##$$abxcyhshbhs##xcznbx##";
s.substring(2, s.length());
This doesn't do any pattern matching to look for the $$.
I need to dynamically check for presence of char sequence "(Self)" in a string and parse it out.
So if my string say myString is
"ABCDEF (Self)"
it should return myString as
"ABCDEF"
What is the best way of doing it? Can it be done in a single step?
You may use the replace function as follows:
myString = myString.replace(" (Self)","");
Here, read more about things to note with String.replace or the function definition itself. Note that it is overloaded with a char variant, so you can do two kinds of things with a similar function call.
You may use the replaceAll method from the String class as follows:
myString = myString.replaceAll(Pattern.quote("(Self)"), ""));
Try following:
String test="ABCDEF (Self)";
test=test.replaceAll("\\(Self\\)", "");
System.out.println(test.trim());
Output :
ABCDEF
The dig is to use Regular Expressions for more on it visit this link.
And the code won't have a problem if there is no Self in string.
Just check out the String class' public methods.
String modifyString(String str) {
if(str.contains("(Self)")) {
str = str.replace("(Self)", "");
str = str.trim();
}
return str;
}
From the question, I understand that from source string ABCDEF (Self) also the space between F and ( should be removed.
I would recommend to use regEx if you are comfortable with it, else:
String OrigString = "ABCDEF (Self)";
String newString= OrigString.replaceAll("\\(Self\\)", "").trim();
System.out.println("New String : --" +newString+"--");
The Regular Expression for your case would be:
\s*\(Self\)\s*
Tested Java Code using regular expression would be:
String newRegExpString = OrigString.replaceAll("\\s*\\(Self\\)\\s*", "");
System.out.println("New String : -" +newRegExpString+"--");
Output:
New String : --ABCDEF--
New String : -ABCDEF--
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.
In java, I have a string with a date in dd-mm-yyyy format:
String value = "31-01-1989";
Now, I want the value in another variable to be ddmmyyyy format:
String value = "31011989";
How to do this?
In this case you can simply remove dashes
value = value.replace("-", "");
or
value = value.replaceAll("-", "");
but according to my tests the first version is a little bit faster. So I personally prefer to use replaceAll only when the first parameter is a regex.
Note that, despite a confusion in the names, String.replace replaces ALL substrings that match the first arg, just as String.replaceAll does. The main difference is that the String.replace treats the first arg as a string literal and String.replaceAll uses it as a regex.
easy solution:
String value1 = "31-01-1989";
String value2 = value1.replace("-", "");
Have a look at SimpleDateFormat for a general solution. Write a SimpleDateFormat to parse the first date and use format in another to have the expected output.
You can use String.replace(Charseq, Charseq) to remove the delimiters.
String value = "31-01-1989";
String value2 = value.replace("-", "");
System.out.println(value2);
abcd+xyz
i want to split the string and get left and right components with respect to "+"
that is i need to get abcd and xyz seperatly.
I tried the below code.
String org = "abcd+xyz";
String splits[] = org.split("+");
But i am getting null value for splits[0] and splits[1]...
Please help..
The string you send as an argument to split() is interpreted as a regex (documentation for split(String regex)). You should add an escape character before the + sign:
String splits[] = org.split("\\+");
You might also find the Summary of regular-expression constructs worth reading :)
"+" is wild character for regular expression.
So just do
String splits[] = org.split("\\+");
This will work
the expression "+" means one or many in java regular expression.
split takes Regex as a argument hence the comparion given by you fails
So use
String org = "abcd+xyz";
String splits[] = org.split(""\+");
regards!!
Try:
String splits[] = org.split("\\+");