The split() method in Java does not work on a dot (.)
java.lang.String.split
splits on regular expressions, and .
in a regular expression means "any character".
Try temp.split("\\.")
.
Java .split(|) not working
Use escape character before | like below:
String[] res = "12345|6".split("\\|");
Similar "escape character logic" is required, when you are
dealing/splitting with any of the below special characters (used by
Regular Expression):
- OR sign (|)
- question mark (?)
- asterisk (*)
- plus sign (+)
- backslash (\)
- period (.)
- caret (^)
- square brackets ([ and ])
- dollar sign ($)
- ampersand (&)
String.split on dot doesn't work as expected
split
uses regex and in regex .
means "any character beside line separators".
So you split on each character creating array full of empty elements like
"foo".split(".")
would at first create ["","","",""]
, but since split
also trails empty elements placed at the end of array you would get empty array []
. Trailing last empty strings can be turned off with overloaded version of split split(regex,limit)
by passing negative value as limit
.
To solve this problem you need to escape .
. To do this you can use for instance
split("\\.")
- standard escape in regexsplit("[.]")
- escaping using character classsplit("\\Q.\\E")
-\Q
and\E
mark area in which regex metacharacters should be treated as simple literalssplit(Pattern.quote("."))
- this method usesPattern.LITERAL
flag inside regex compiler to point that metacharacters used in regex are simple literals without any special meaning
Another problem would be condition in your for
loop but more about it in Jeroens answer.
Java string split with . (dot)
You need to escape the dot if you want to split on a literal dot:
String extensionRemoved = filename.split("\\.")[0];
Otherwise you are splitting on the regex .
, which means "any character".
Note the double backslash needed to create a single backslash in the regex.
You're getting an ArrayIndexOutOfBoundsException
because your input string is just a dot, ie "."
, which is an edge case that produces an empty array when split on dot; split(regex)
removes all trailing blanks from the result, but since splitting a dot on a dot leaves only two blanks, after trailing blanks are removed you're left with an empty array.
To avoid getting an ArrayIndexOutOfBoundsException
for this edge case, use the overloaded version of split(regex, limit)
, which has a second parameter that is the size limit for the resulting array. When limit
is negative, the behaviour of removing trailing blanks from the resulting array is disabled:
".".split("\\.", -1) // returns an array of two blanks, ie ["", ""]
ie, when filename
is just a dot "."
, calling filename.split("\\.", -1)[0]
will return a blank, but calling filename.split("\\.")[0]
will throw an ArrayIndexOutOfBoundsException
.
split() function for '$' not working
String.split()
takes in regex as argument and $
is a metacharacter in Java regex API
. Therefore, you need to escape it:
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("\\$")[i]);
}
Other metacharacters supported by the Java regex API
are: <([{\^-=!|]})?*+.>
Split function with dot on string in java
Try this
String arr[] = dob.split("\\.");
ie, you need to put the double slash to escape the dot as dot will match any character in regex. Also note that double backslash is used to create a single backslash in regex.
String split in java does not work?
String input = ":-) :) :o) :] :3 :c) :> =] 8) =) :} :^)";
String[] similies = input.split(" ");
for (String simili : similies) {
System.out.println(simili);
}
This works fine.
Output :
:-)
:)
:o)
:]
:3
:c)
:>
=]
8)
=)
:}
:^)
and in case if there is any tab/newline/spaces and you wnat to split, in that case you can use
input.split("\\s+");
in your example there is few more charaters are their like  and non breaking whitespaces so you have to explicitly handle these type of charater. Here is the code:
public static void main(final String[] args) throws Exception {
BufferedReader fileReader = new BufferedReader(new FileReader("emoticon.txt"));
String line = "";
while ((line = fileReader.readLine()) != null) {
line = line.replaceAll("Â", "");
line = line.replace("" + ((char) 160), " ");
System.out.println("line: " + line);
String[] icons = line.split("\\s+");
for (String icon : icons) {
System.out.println(icon);
}
System.out.println("=======================");
}
}
String's split function doesn't
You need to escape the period, i.e., it must be temp.split("\\.")
.
Otherwise, it will treat period as "any character".
Since any character matches all characters of your string, your complete string becomes only separators. Since separators are left out of the split result, the split result is empty. This is why you get the out of bounds exception.
Related Topics
Differences Between Java 8 Date Time API (Java.Time) and Joda-Time
What Does Class<> Mean in Java
Passing Pointers Between C and Java Through Jni
How to Find Out What Keystore My Jvm Is Using
Jackson: How to Prevent Field Serialization
Java - Why No Return Type Based Method Overloading
How to Get Whole and Fractional Parts from Double in Jsp/Java
Play a Youtube Video Using Javafx
How Is the Fork/Join Framework Better Than a Thread Pool
Springboot - Making Jar Files - No Auto Configuration Classes Found in Meta-Inf/Spring.Factories
Printing Even and Odd Using Two Threads in Java
Exception in Thread 'Main' Java.Lang.Noclassdeffounderror:
How to View Tomcat Log Files in Eclipse
Why Doesn't String's Hashcode() Cache 0
Session.Connection() Deprecated on Hibernate
How to Convert Date in to Yyyy-Mm-Dd Format
Is There a Good Reason to Use "Printf" Instead of "Print" in Java