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("\\|");
Related
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(" ");
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("\\*");
I want to split a file with a pipe character on a string like number|twitter|abc.. in the mapper.
It is a long string. But it doesn't recognize pipe delimiter when I do:
String[] columnArray = line.split("|");
If I try to split it with a space like line.split(" "), it works fine so I don't think there is a problem with it recognizing characters.
Is there any other character that can look like pipe? Why doesn't split recognize the | character?
As shared in another answer
"String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean."
https://stackoverflow.com/a/9808719/2623158
Here's a test example.
public class Test
{
public static void main(String[] args)
{
String str = "test|pipe|delimeter";
String [] tmpAr = str.split("\\|");
for(String s : tmpAr)
{
System.out.println(s);
}
}
}
String.split takes a regular expression (as the javadoc states), and "|" is a special character in regular expressions. try "[|]" instead.
I am trying to parse a file that has each line with pipe delimited values.
It did not work correctly when I did not escape the pipe delimiter in split method, but it worked correctly after I escaped the pipe as below.
private ArrayList<String> parseLine(String line) {
ArrayList<String> list = new ArrayList<String>();
String[] list_str = line.split("\\|"); // note the escape "\\" here
System.out.println(list_str.length);
System.out.println(line);
for(String s:list_str) {
list.add(s);
System.out.print(s+ "|");
}
return list;
}
Can someone please explain why the pipe character needs to be escaped for the split() method?
String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean.
Because the syntax for that parameter to split is a regular expression, where in the '|' has a special meaning of OR, and a '\|' means a literal '|' so the string "\\|" means the regular expression '\|' which means match exactly the character '|'.
You can simply do this:
String[] arrayString = yourString.split("\\|");
I have a String, which I want to split into parts using delimeter }},{". I have tried using:
String delims="['}},{\"']+";
String field[]=new String[50];
field=subResult.split(delims);
But it is not working :-( do you know, what expression in delims should I use?
Thanks for your replies
A { is a regex meta-character which marks the beginning of a character class. To match a literal { you need to escape it by preceding it with a \\ as:
String delims="}},\\{";
String field[] = subResult.split(delims);
You need not escape the } in your regex as the regex engine infers that it is a literal } as it is not preceded by a opening {. That said there is no harm in escaping it.
See it
If the delimiter is simply }},{ then subResult.split("\\}\\},\\{") should work
String fooo = "asdf}},{bar}},{baz";
System.out.println(Arrays.toString(fooo.split("\\}\\},\\{")));
You should be escaping it.
String.split("\\}\\},\\{");
You could be making it more complex than you need.
String text = "{{aaa}},{\"hello\"}";
String[] field=text.split("\\}\\},\\{\"");
System.out.println(Arrays.toString(field));
Use:
Pattern p = Pattern.compile("[}},{\"]");
// Split input with the pattern
String[] result = p.split(MyTextString);