Splitting string gives null result in java - java

i am trying very simple splitting.
I dont know why it is not working.
String abc= "192.168.120.2";
String[] eachByteabc= abc.split(".");
When I debug it and see, I get the result that abc contains : 192.168.120.2.
But when I do split, it does not give me error but gives me null result.
I think, i have made some silly mistake.
Can you tell me where I am wrong. What should I do.
Thank you in advance.

Try it =):
String[] eachByteabc= abc.split("\\.");

You need to escape the ., since it's a regex operator. Change it to:
String[] eachByteabc= abc.split("[.]");
Addition, thanks to #sparks:
While this will work, the [] characters in regex are used to annotate
a set, so if you are looking for where it might be in a limited series
of characters, you should use them.
In this case - use \\. to escape the . character.

public String[] split(String regex) takes a regular expression as an argument. You must escape the point, since it's a regex operator.

String[] eachByteabc = abc.split("."); is not eorr,but you can not to debug and Watch the values.use String[] eachByteabc = abc.split(".");you can Watch values in the debug.

String.split uses regex, so you need to use abc.split("\\.");
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

Related

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(":["));

Java regex for inserting text between braces

I have a String
a = "stringWithBraces()"
I want to create the following string
"stringWithBraces(text)"
How do I achieve this using regex?
I tried this :
a.replaceAll("\\(.+?\\)", "text");
But get this :
stringWithBraces()
You can use lookaheads and do something like this:
(?<=\().*?(?=\))
Live Demo
Thus doing this:
String a = "stringWithBraces()";
a = a.replaceAll("(?<=\\().*?(?=\\))", Matcher.quoteReplacement("text"));
System.out.println(a);
Outputs:
stringWithBraces(text)
Note that in relation to replaceAll() then the replacement string has some special character. So you should most likely use Matcher.quoteReplacement() in order to escape those and be safe.
You can use this :
a = a.replaceAll("\\((.*?)\\)", "(text)");
You have to replace every thing between parenthesis with (text)
+ requires at least one char, the ? added here means the shortest match, so "...(.)...(.)..." would not continue to find ".)...(.".
a.replaceAll("\\(.*?\\)", "(text)");
You might have intended replaceFirst; though I think not.
You might also let the dot . match new line chars, for mult-line matches,
using the DOT_ALL option (?s):
a.replaceAll("(?s)\\(.*?\\)", "(text)");

split function doesn't return

String str ="|m4oho5kspqikkfn2may72osnfzmn3gutwzqctblzqy6rygwzxbra6bjkmy|113|70|";
String[] tokens = str.split("|");
System.out.println(tokens[0]);
System.out.println(tokens[1]);
result in white::
0
I just need this
But the only thing I want to come back is: m4oho5kspqikkfn2may72osnfzmn3gutwzqctblzqy6rygwzxbra6bjkmy
Sorry not be much English I am using Google Translator
| is a regex protected character. You need to escape it when splitting like so:
str.split("\\|");
Regards
You can find more detailed information for regex expression in the java doc:
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
There are a lot of really good examples. I strongly recommend to check them.
For literals you must use double backslash (\) to escape the escaped character.
Cheers.

How to search for a special character in java string?

I am having some problem with searching for a special character "(".
I got a java.util.regex.PatternSyntaxException exception has occurred.
It might have something to do with "(" being treated as special character.
I am not very good with pattern expression. Can someone help me properly search for the escape character?
// I need to split the string at the "("
String myString = "Room Temperature (C)";
String splitList[] = myString.split ("("); // i got an exception
// I tried this but got compile error
String splitList[] = myString.split ("\(");
Try one of these:
string.split("\\(");
string.split(Pattern.quote("("));
Since a string split takes a regular expression as an argument, you need to escape characters properly. See Jon Skeet's answer on this here:
The reason you got an exception the first time is because split() takes a regular expression as argument, and ( has a special meaning there, as you suggest. To avoid this, you need to escape it using a \, like you tried.
What you missed, is that you also need to escape your backslashes with an extra \ in Java, meaning you need a total of two:
String splitList[] = myString.split ("\\(");
You need to escape the character via backslashes: string.split("\\(");
( is one of regex special characters. To escape it you can use e.g.
split(Pattern.quote("(")),
split("\\Q(\\E"),
split("\\("),
split("[(]").

Split a string with regular expression

How can I split string for 3 character? (I don't want to do loop for this, maybe some regular expression will be help)
I give example:
String str = "111222333444";
String[] result = str.split("help?"); // get "111", "222", "333"
Using guava-library
Iterable<String> strNums = Splitter.fixedLength(3).split("111222333444")
Readable than using regex. You can then use Ints.tryParse(...) to get Integer version if you want.
Using .split will match regular expressions in the string which, in the underlying implementation, involves traversing the entire string anyway. Writing a simple loop to just create a token from every 3 characters would probably be more efficient.
Frankly, I don't think you can do it for a string of undefined length, without a loop.
You can not use split because the arg of split is the separator, not the resulting sub-strings.
So, your separator regex would be nothing !?
Sorry, you heve write loop. BTW, the regex engine for splitis full of loops.

Categories