This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Split a String on | (pipe) in Java [duplicate]
(3 answers)
Closed 8 years ago.
I'm trying to split the following: 116,24|426,776 into two strings upon the | symbol.
My code:
String[] splitString = "116,24|462,776".split("|");
System.out.println(splitString[1]);
However, this code simply returns: 1
Any clues?
You have to use escape character.
Try this String[] splitString = "116,24|462,776".split("\\|");
Here \\ is the escape character.
Please refer http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Related
This question already has answers here:
How do I get the file extension of a file in Java?
(33 answers)
Split string with dot as delimiter
(13 answers)
Closed 2 years ago.
Split method not working with "." in android and java .
String[] extension = selectedItem.getmName().split(".");
Try :
String[] extension = FilenameUtils.getExtension(selectedItem.getName())
Reference : How do I get the file extension of a file in Java?
Simply escape the ".":
String[] sentences = selectedItem.getmName().split("\\.");
Since "." is regex for any character the result would always be an array of 0 length since every character is a delimeter and split only returns what is not a delimeter - which would be nothing.
String[] sentences = selectedItem.getmName().split("\\.");
This must works. This is a special situation for dot.
This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?
Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 3 years ago.
I am trying to split a string as follows, "Client|Role". My regex is "|".
I expected two substrings as "Client" and "Role", but what I get is each and every character as a substring including the character in my regex. Why I am getting like this? Please help, Thanks in advance.
My code is as follows,
String output = "Client|Role";
String values[] = output.split("|");
You can split like this..
String b = "Client|Role";
String[] elements = b.split("\\|");
This question already has answers here:
Trim characters in Java
(14 answers)
Trim leading or trailing characters from a string?
(6 answers)
Closed 5 years ago.
Consider this code:
string xx = "/test/file2/".Trim('/');
Console.WriteLine(xx);
Console.Read();
This code returns test/file2. What is an efficient way to do this in Java?
Here is how to do the same in Java:
String xx = "/test/file2/".replaceAll("(^/*|/*$)", "");
This uses a regex that matches multiple / from the beginning or multiple / at the end and replaces them with void.
This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I try to split "." but it can not work, I get strings.length equals 0 .What's wrong with it?
String string = "11.12.1";
String[] strings = string.split(".");
As string split takes an regex as argument, . is a wildcard for any character. Just escape it using a backslash (which you have to also escape for java with another one). Additionally, as Youcef Laidani pointed out, you have to call split on the string you just created, not something else:
string.split("\\.");