use split() to split string like "004*034556" - java

In my project I used the code to split string like "004*034556" , code is like below :
String string = "004*034556";
String[] parts = string.split("*");
but it got some error and force closed !!
finally I found that if use "#" or another things its gonna work .
String string = "004#034556";
String[] parts = string.split("#");
how can I explain this ?!

Your forgetting something very trivial.
String string = "004*034556";
String[] parts = string.split("\\*");
I recommend you check out Escape Characters.

Use Pattern.quote to treat the * like the String * and not the Regex * (that have a special meaning):
String[] parts = string.split(Pattern.quote("*"));
See String#split:
public String[] split(String regex)
↑

Refer JavaDoc
String[] split(String regex)
Splits this string around matches of the given regular expression.
And the symbol "*" has a different meaning when we talk about Regex in Java
Thus you would have to use an escape character
String[] parts = string.split("\\*");

Related

I need suggestion to split word and special characters in string

My String is as below
String responseBody = ["{\"event\":{\"commonEventHeader\":{\"sourceId\":\"\",\"startEpochMicrosec\":\"1590633627120000\",\"eventId\":\"135.16.61.40-Fault_bgp_neighbor_adjacency_down-192.20.126.67\",\"internalHeaderFields\"}"]
I want to split this string by event\":
I am trying below :
String[] json = responseBody.split("event\":");
This is not able to split , I am not getting any error too . Please suggest .
I'm confused as to why you wouldn't try to parse the JSON since it looks like you know it is JSON. But in the spirit of answering the actual question, I think it's because the string you are trying to split actually contains the \ character, and therefore you should use:
String[] json = responseBody.split("event\\\\\":");
Why so many \? Well the actual regex is event\\": but in Java, escape each \ and the ".
Use Built-in String method split("regex").
Example:
String s = "This is a String";
//Split String
String[] arr = s.split(" ");

Java: Split String around "+" [duplicate]

This question already has answers here:
Java - How to split a string on plus signs?
(2 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Just found out, that I get a NullPointerException when trying to split a String around +, but if I split around - or anything else (and change the String as well of course), it works just fine.
String string = "Strg+Q";
String[] parts = string.split("+");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
Would love to hear from you guys, what I am doing wrong!
This one works:
String string = "Strg-Q";
String[] parts = string.split("-");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
As + is one of the special regex syntaxes you need to escape it.
Use
String[] parts = string.split("\\+");
Instead of
String[] parts = string.split("+");
Try this:
final String string = "Strg+Q";
final String[] parts = string.split("\\+");
System.out.println(parts[0]); // Strg
System.out.println(parts[1]); // Q
It happens because + is a special character in Regex - it's the match-one-or-more quantifier.
The only thing you need to do is to escape it:
String[] parts = string.split("\\+");
String.split(...) expects a Regular Expression and the '+' is a special character. Try:
String string = "Strg+Q";
String[] parts = string.split("[+]");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
+ is a special regex character which means that you need to escape it in order to use it as a normal character.
String[] parts = string.split("\\+");
The problem you got here is that "+" is a meta character in Java, so you need to "scape" it by using "\\+".
As others have mentioned, '+' is special when dealing with regex. In general, if a character is special you can use Pattern.quote() to escape it, as well as "\\+". Documentation on Pattern: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Example:
String string = "Strg-Q";
String[] parts = string.split(Pattern.quote("+"));
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Qenter code here
Remember to import pattern from java.util.regex!
Amit beat me too it. string.split() takes a regular expression as its argument. In regexes + is a special symbol. Escape it with a backslash. Then, since it's a java string, escape the backslash with another backslash, so you get \+. Try testing your regular expressions on an online regex tester.

How to Split String With double quotes in java [duplicate]

This question already has an answer here:
Divide/split a string on quotation marks
(1 answer)
Closed 8 years ago.
This question is Pretty Simple
How to Split String With double quotes in java?,
For example I am having string Do this at "2014-09-16 05:40:00.0",After Splitting, I want String like
Do this at
2014-09-16 05:40:00.0,
Any help how to achieve this?
This way you can escape inner double quotes.
String str = "Do this at \"2014-09-16 05:40:00.0\"";
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
Output
Do this at
2014-09-16 05:40:00.0
Use method String.split()
It returns an array of String, splitted by the character you specified.
public static void main(String[] args) {
String test = "Do this at \"2014-09-16 05:40:00.0\"";
String parts[] = test.split("\"");
String part0 = parts[0];
String part1 = parts[1];
System.out.println(part0);
System.out.println(part1);
}
output
Do this at
2014-09-16 05:40:00.0
Try this code. Maybe it can help
String str = "\"2014-09-16 05:40:00.0\"";
String[] splitted = str.split("\"");
System.out.println(splitted[1]);
The solutions provided thus far simply split the string based on any occurrence of double-quotes in the string. I offer a more advanced regex-based solution that splits only on the first double-quote that precedes a string of characters contained in double quotes:
String[] splitStrings =
"Do this at \"2014-09-16 05:40:00.0\"".split("(?=\"[^\"].*\")");
After this call, split[0] contains "Do this at " and split[1] contains "\"2014-09-16 05:40:00.0\"". I know you don't want the quotes around the second string, but they're easy to remove using substring.

How to split string separated by | character

I have input string in the following format
first|second|third|<forth>|<fifth>|$sixth I want to split this string into an array of string with value [first,second,third,,,$sixth]. I am using following code to split the string but that is not working. please help me.
public String[] splitString(String input){
String[] resultArray = input.split("|")
return resultArray;
}
Could you please tell me what am I doing wrong.
You need to escape | using backslash as it is a special character. This should work:
String[] resultArray = input.split("\\|")
| is a meta character meaning it represents something else in regex. Considering split takes regex as an argument, it interprets the argument using regex. You need to "escape" all of the meta characters by placing a \\ before it. In your case, you would do:
String[] resultArray = input.split("\\|");

Split the string

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("\\+");

Categories

Resources