Split a string using a string as delimiter - java

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.

Related

How to replace special character }} in a string with "" using regexp in java

I am trying to replace special character }} in a string with "" using regexp in Java, I tired the below two methods and it doesn't work. Please let me know what is wrong with these statements.
Note the string would also contain } which I would like to retain. Goal is to replace only }}.
Method 1:
String buffer = obj.toJSONString() + ",";
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
Method 2:
Pattern.compile("(?<![\\w\\d])}}(?![\\w\\d])").matcher(buffer).replaceAll("");
The quote in the following:
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
says to treat the regex as a literal string. That's wrong.
If you simply want to remove all }} irrespective of context:
String result = buffer.replaceAll(Pattern.quote("}}"), "");
If you do need to respect the context, don't Pattern.quote(...) the regex!
The other problem is in the way that you attempt to specify the character classes. Since \d is a subset of \w, it is unnecessary to combine them. Just do this instead:
String result = buffer.replaceAll("(?<!\\w)\\}\\}(?!\\w)"), "");
I'm not sure if it is strictly necessary to quote the } characters, but it is harmless if it is not necessary.
Dont' use Pattern.quote, use a literal regex pattern, and escape the brackets:
Stringbuffer = obj.toJSONString() + ",";
String result = buffer.replaceAll("(?<![\\w\\d])\\}\\}(?![\\w\\d])", "");
Using Pattern.quote tells the regex engine to treat the string as literal. This does mean the brackets would not have to be escaped, but it would also render your lookarounds as literal text, probably not what you have in mind.
The method 2 still needs to escape special characters }
Pattern.compile("(?<![\\w\\d])\\}\\}(?![\\w\\d])").matcher(buffer).replaceAll("");
Can you please try same with Apache StringUtils. It’s faster and should work in your case. Kindly find following links for reference.
apache-stringutils-vs-java-implementation-of-replace
Apache StringUtils 3.6

Unable to Split by ":[" in Java

I have 2 nested HashMaps as a String which I am trying to parse.
My String is as follows :
"20:[cost:431.14, Count:19, Tax:86.228"
Therefore I need to Split by ":[" in order to get my key, 20, For some reason I'm not able to do this.
I have tried :
myString.split(":[") and myString.split("\\:[") but neither seem to work.
Can anyone detect what I have wrong here?
Thanks in Advance
You have to escape the character [ , but not the character : like below:
String str = "20:[cost:431.14, Count:19, Tax:86.228";
String[] spl = str.split(":\\[");
String.split use regex.
Splits this string around matches of the given regular expression.
You need to escape [ since this is a "reserved" character in regular expresionn, not :
myString.split(":\\[")
Not that you could/should set a limit if you only want the first cell
myString.split(":\\[", 2);
This will return an array of 2 cell, so after the first occurence, it doesn't need to read the rest of the String. (This is not really necessary but good to know).
Use Pattern.quote to automatically escape your string
String string = "20:[cost:431.14, Count:19, Tax:86.228";
String[] split = string.split(Pattern.quote(":["));
Another solution :
Therefore I need to Split by ":[" in order to get my key, 20. For
some reason I'm not able to do this.
In this case you can use replaceAll with some regex to get this input so you can use :
String str = "20:[cost:431.14, :[Count:19, Tax:86.228";
String result = str.replaceAll("(.*?):\\[.*", "$1");// output 20
regex demo
If the key is just an integer you can use (\d+):\[ check regex demo
be noted '[' character is special character in regular expression so you have to make an escape character like \\ str.split(":\\["); and remember the string is immutable so if do you want to use it twice you have to reassign it with split like this String[] spl =str.split(":\\[");
Another solution if you just need the key "20" in your String is to substring it to get the part before the delimiter.
String key = myString.substring(0, myString.indexOf(":["));

Regex to remove string between two characters (exclusive)

If I have a string blah.t!#Z8-st? how can I remove the string between the characters . and ? in Java?
so the resulting text would be blah.?
I have got the following but it is inclusive:
String s = "blah.t!#Z8-st?";
System.out.println(s.replaceAll("\\..*?\\?", ""));
Since this looks like a contest to "write the most complex regex", let me submit my modest contribution.
System.out.println("blah.t!#Z8-st?".replaceAll("\\..*?\\?", ".?"));
Update
After your edit it appears that you already have the exact same regex as mine, so the only thing you need to fix is the "replace with" part, where you keep the delimiters.
You can use this:
String repl = string.replaceAll("^(.*?\\.)[^?]*", "$1");
Use String replaceAll method with following pattern:
Pattern
(?<=\.).*(?=\?)
Code
String s = "blah.t!#Z8-st?asdas";
System.out.println(s.ReplaceAll("(?<=\\.).*(?=\\?)", ""));

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 "\\?" in Java [duplicate]

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.

Categories