How to split string into sentences in android and java? [duplicate] - java

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.

Related

java.lang.ArrayIndexOutOfBoundsException after two splits [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I want to split a string three times.
This is the string:
21.06.2016;00:30
My function looks like this:
String[] split = dateV.split(";");
String[] date = split[0].split(".");
String[] time = split[1].split(":");
date[0] should contain "21" after all
So the first part works great.
My two strings are
split[0] = 21.06.2016
split[1] = 00:30
But when I call split[0].split("."); I get a
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
Can somebody tell me why?
String.split uses regular expressions to do the split, and a dot is a special character when regular expressions are used.
To split using a dot, you need to escape it like this
String[] date = split[0].split("\\.");

Java Spit "." cannot work [duplicate]

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

How to convert dot (.) separated string into array list? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
In my application I need to convert dot separated string to arraylist. I have seen in many places split function of string is used to separate comma separated string to Array List.I studied in the documentation split function requires expression for splitting.In my case I provided dot and it is not converting.Am I missing something? Please help me.Below is my code I am using
Here,
groupHierarchy.levelPrefix = 4201.4202;
ArrayList<String> levelPrefixList = new ArrayList<>(
Arrays.asList(groupHierarchy.levelPrefix.split(".")));
When I log this it shows
Log.e("tracking","level prefix list is "+ levelPrefixList);
Use following:
groupHierarchy.levelPrefix.split("\\.")
public String[] split(String regex) use regex, so you have to escape "."
See Split String on . as delimiter for more details.

Extract the data Using regular Expression in java [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 7 years ago.
I have the unproper data in this way. i need to extract the data before dot and after dot symbol using regular expression. I am using but i am not able to get exact data. please help. It is very urgent
Code:
Matcher matcher = Pattern.compile("([\\w[\\$##\\-^&]\\w\\[\\]' $]+)\\.([\\w\\[\\]' $]+)").matcher(formulaData);
while (matcher.find())
{
String Data=matcher.group(0);
String[] pieces = Data.split("\\.");
Heading=pieces[0].replace("\"", "");
Heading=pieces[1].replace("\"", "");
}//while
You can split by newline and then split by dot

split string not working in java [duplicate]

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

Categories

Resources