This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 5 years ago.
String line = "First string March 8, # 2017: Boris#|#Second string";
String[] list = line.split("#|#");
i was expecting list[0] = "First string March 8, # 2017: Boris" and
list[1] = "Second string"
But i am not getting the result as expected . its get split to multiple strings. whats the change i need to do in split function ?
String[] list = line.split("#\\|#");
The split() method's (first) parameter is expected to contain a regular expression. The | is a special character is Regex, so you need to escape it with \ to represent it in a regex literally.
You need to escape the pipe: #\\|#
example:
String line = "First string March 8, # 2017: Boris#|#Second string";
String[] list = line.split("#\\|#");
System.out.println(Arrays.toString(list));
The split() method doesn't expect normal strings, but regular expressions. Thus you need to escape the | char; so go for:
split("#\\|#");
Related
This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 4 years ago.
I have a below string which is coming from the server
String text = "- 30016264\n- 30014837\n- 30014836\n";
When I used to split it like this
String[] list = text.split("\n");
I got the list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
And when I used to split it like this
String[] list = text.split("\\n");
I got the same list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
How do I write the code to split the string on basis of "\n" not the next line?
NOTE: This string is coming from the server as it is written here and when I use this server string as TextView value, it will display in one single line.
If you input is coming from the server and in this format :
- 30016264\n- 30014837\n- 30014836\n
Then, in Java it should be represented with double backslash like this :
- 30016264\\n- 30014837\\n- 30014836\\n
because backslash is a special character in Java, you have to escape it with another backslash.
Then to split with \\n you need to use \\\\n, why 4 backslashes because like i said before the backslash is special character for that you have to escape each one with another backslash for that you need 4 instead of 2 or 1.
Your solution should look like :
String text = "- 30016264\\n- 30014837\\n- 30014836\\n";
String[] split = text.split("\\\\n");
Outputs
- 30016264
- 30014837
- 30014836
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 5 years ago.
There is a string which I trying to parse by "|" symbol:
1-20|21-40|41-60|61-80|81-100|101-120|121-131
String[] arr = text.split("|");
for(int i = 0; i <arr.length; i++){
System.out.println( arr[i] );
}
It parses to every character, like
1
-
2
0
|
2
1
...
How to parse the source string for elements like:
1-20
| is a special character in Java's regex syntax that means a logical "or" between two matching groups. If you want to match the | literal, you need to escape it:
String[] arr = text.split("\\|");
This | is a special character in regular expression(s), you need to escape it. Like,
String[] arr = text.split("\\|");
| is a metacaracter in regex. Escape it:
String[] splitValues = text.split("\\|");
escape the pipe using "\\|"
String[] arr = text.split("\\|");
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Split string with | separator in java
I'm little confused as when i do the following:
String example1 = "Hello|World";
String[] splitRes;
splitRes = example1.split("|");
I don't get split string
Hello index 0
World index 1
But if I'll do
String example1 = "Hello:World";
String[] splitRes;
splitRes = example1.split(":");
then it works..
Why is it happening?
split uses a regex, you must escape the pipe because it is a "or" operator in regex:
example1.split("\\|");
String.split() expects regular expression as argument, | is a meta character "OR" in regular expression. You have to escape with \ (so it becomes \|). Note that in Java string, you have to write it as \\ since \ is also an escape character in Java string.
| is used in regular expression, .split also use regular expression so you need to escape it.
String str = ""Hello:World"; ";
String[] temp;
String delimiter = "\\|";
SepString= str.split(delimiter);
/* print test */
for(int i =0; i < SepString.length ; i++)
System.out.println(SepString[i]);
Split takes a regexp as an argument, | is a a regexp symbol.
You would have to escape it using \ which in a java string is two of them: \\
.split("\\|");
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html