How to split String without regex [duplicate] - java

This question already has answers here:
String.split() *not* on regular expression?
(8 answers)
Closed 2 years ago.
In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.
Finding indices (by indexOf and lastIndexOf) does not use regex, so
String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3
gives me the index I need.
However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...
String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need either of the following:
A way to split into an array by a non-regex target
A way to "convert" a non-regex target string into a regex string
Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?

You need to escape your "target" in order to use it as a regex.
Try
String[] someStringSplit = someString.split(Pattern.quote(target));
and let me know if that helps.

String::split do split without regex if the regex is:
a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
two-char String and the first char is the backslash and the second is
not the ascii digit or ascii letter.
Please see String::split() source code for details.
For escaped '.' target it is going to be split without regex.

You can try this one.
String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);
while (tokenValue.hasMoreTokens()) {
System.out.println(tokenValue.nextToken());
}

Related

Java string tokenizer delimiter [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I have a string "domain\cdsid", where "\" is the delimiter, all I want is to split the string and just print "cdsid".
Input string : "domain\cdsid"
Output string : "cdsid"
How do I do this ?
Try this (using split) :
String myText = "domain\\cdsid";
System.out.println(myText.split("\\\\")[1]);
Output :
cdsid
In Java String object "\" is used to define any escape sequence character like \n for new line, \t for tab, \\ for having a backslash in the String object.
So instead of writing String object as
String str = "domain\cdsid";
You have to write
String str = "domain\\cdsid";
The first option will give compile time error. Java will expect that after backslash their must be some escape sequence character but it is not their in first case. It will compile time error as
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
In the above compile time error each separate value is a escape sequence character in java.
So your final code will be
String str = "domain\\cdsid";
System.out.println(str.split("\\\\")[1]);
Hope this helps.
Splitting is a way that I will recommended to go when you need all the elements resulting of the operation... this is because the result will generate an Array of Strings (what a waste of memory generating an array to only get ONE element! :) dont you think??)
in your case something like regex or just substrings will gently provide you the correct answer..
consider:
String txt = "domain\\cdsid";
System.out.println(txt.substring(txt.indexOf("\\") + 1));
output:
cdsid

String whitespace clean up that is not trim [duplicate]

This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 8 years ago.
so I have been looking on here and I can find alot of solutions that either completely remove all white space or just remove spaces, or just remove tabs. Basically what I need/ want is a way to take a string, and turn all double spaces+ or tabs and turn them into a single space. ie
String temp = "This is a test for strings";
String result = "This is a test for strings";
any ideas? possible java library methods?
Use String.replaceAll:
String result = temp.replaceAll("\\s+", " ");
where \\s+ stands for more than one whitespace character.
You can use regExp with method #replaceAll other than that you can first use trim to remove leading and trailing spaces.
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " ");
Here \\s+ is regExp which means one or more spaces which will be replaced with single space by replaceAll method.
Try this:
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " "));

Can't match two different regex on split

So I'm using a String as a delimiter to use when I call the Split method.
String[] aExpr;
String strDelimiter = "[-+/=^//%//*//(//);:?]";
aExpr = expr.split(strDelimiter);
This fills aExpr with the strings broken accordingly with the strDelimiter.
The thing is that I also want the Split() method to compare not only the strDelimiter, but also this String:
String oprDelimiter = "[abcdefghijklmnopqrstuvwxyz0123456789]+"
Which is basically any characters followed by numbers. I could add all these characters to the First String, but the + in the end won't let me. The + means that any combination of the words will break the String. Any ideas of how could I do this?
Try using this regex:
(?<=[abcdefghijklmnopqrstuvwxyz0123456789])(?=[-+=^%*();:?])|(?<=[-+=^%*();:?])(?=[abcdefghijklmnopqrstuvwxyz0123456789])
as the delimiter. It will split on any location that is preceded by any of the characters abcdefghijklmnopqrstuvwxyz0123456789 and followed by any of the characters -+=^%*();:?, or vice versa. Explanation and demonstration here: http://regex101.com/r/mT3lL1.

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

Is it possible to split a String around "." in java?

When I try to split a String around occurrences of "." the method split returns an array of strings with length 0.When I split around occurrences of "a" it works fine.Does anyone know why?Is split not supposed to work with punctuation marks?
split takes regex. Try split("\\.").
String a = "a.jpg";
String str = a.split(".")[0];
This will throw ArrayOutOfBoundException because split accepts regex arguments and "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0]; //Yes, two backslashes
When the code is compiled, the regular expression is known as "\.", which is what we want it to be
Here is the link of my old blog post in case you are interested: http://junxian-huang.blogspot.com/2009/01/java-tip-how-to-split-string-with-dot.html

Categories