This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I want to split an android string to smaller ones with any | char.
Just imagine I have this long string :
This|is|a|long|string|in|java
So, I wanna split it. I need to get a array in output with this values :
[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"
I have tried :
separated = oldstring.split("|");
But, i didn't give me the thing i need!
How can i do that? Any code can do that?
Note that String's split() method take regex as a param. Not string.
public String[] split(String regex)
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String separated[] = oldstring.split("\\|");
Related
This question already has answers here:
Regexp to remove specific number of occurrences of character only
(2 answers)
Closed 2 years ago.
I was wondering how I could split a String by : but not :: using String#split(String)
I am using Java if it makes a difference.
I looked around a lot and I couldn't find anything, and I'm not familiar with Regex...
Example:
coolKey:cool::value should return ["coolKey", "cool::value"]
cool::key:cool::value should return ["cool::key", "cool::value"]
You could try splitting on (?<!:):(?!:):
String input = "cool::key:cool::value";
String[] parts = input.split("(?<!:):(?!:)");
System.out.println(Arrays.toString(parts));
This prints:
[cool::key, cool::value]
The regex used here says to split when:
(?<!:) the character which precedes is NOT colon
: split on colon
(?!:) which is also NOT followed by colon
This question already has answers here:
How to Split a mathematical expression on operators as delimiters, while keeping them in the result?
(5 answers)
Closed 4 years ago.
I want to split a mathematical function by the sign of the variables in it like this :
input--> x-5y+3z=10
output--> [x,-5y,+3z,=10]
this code does not work in the way i want :
String function = "x-5y+3z=10";
String split = function.split("=|-|\\+");
the output of the array is :
[x,5y,3z,10]
so what is the correct regex for this ?
The "problem" using split is that the delimiter used will be removed, because it'll takt the parts that are between this delimiter, you need a pattern that is non-capturing or with a simple lookahead : match something wich is before something else
The pattern (?=[-+=]) would work, it'll take the part that starts with a -+= symbol without removing it :
String function = "x-5y+3z=10";
String[] split = function.split("(?=[-+=])");
System.out.println(Arrays.toString(split)); //[x, -5y, +3z, =10]
Some doc on Lookahead
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 6 years ago.
I have a String called filename:
filename = "z_cams_c_ecmf_20170217000000_prod_fc_pl_015_aermr04.nc";
When I try to split the filename to get the variable name aermr04.nc, I tried the following:
String varibleName = filename.split("_")[9].split(".")[0];
The above line of code throws an IndexOutOfBoundsException.
Why?
I can get it tow work by using:
String varibleName = filename.split("_")[9].split("\\.")[0];
However, it seems rather silly that I have to fiddle around with such trivial tasks...
Any idea why the 2nd example works? What is the reasoning behind such syntax?
The argument to .split() is treated as a regular expression. "." as a regex matches everything.
To match a period, you need to escape the "." regex as "\\."
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 9 years ago.
Here If I am given a string aaaa|bbb, I want output as aaaa and bbb. If I use string.split("|") It returns each character of the string as separate Array of strings like
output[0]="a",output[1]="a",output[2]="a",output[3]="a",output[4]="a",output[5]="|",output[6]="b",output[&]="b",output[0]="b"
But I want it as output[0]=aaaa, output[1]=bbb;
Please help me
split() expects a regex, where | has a special meaning and you need to escape it.
string.split("\\|")
You need to escape the | metacharacter:
string.split("\\|")
Use proper escaping: string.split("\\|") or the helper function which has been created for exactly this purpose: string.split(Regexp.quote("|"))
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I would like to parse entire file based on all the possible delimiters like commas, colon, semi colons, periods, spaces, hiphens etcs.
Suppose I have a hypothetical string line "Hi,X How-how are:any you?" I should get output array with items Hi,X,How,how,are,any and you.
How do I specify all these delimiter in String.split method?
Thanks in advance.
String.split takes a regular expression, in this case, you want non-word characters (regex \W) to be the split, so it's simply:
String input = "Hi,X How-how are:any you?";
String[] parts = input.split("[\\W]");
If you wanted to be more explicit, you could use the exact characters in the expression:
String[] parts = input.split("[,\\s\\-:\\?]");