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 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 by dot - Java
This handles all delim values:
String str = "21.12.2015";
String delim = "."; // or "-" or "?" or ...
String[] st = str.split(java.util.regex.Pattern.quote(delim));
String split with Dot character not working
Did you escape the dot? string.split("\\.")
or string.split("[.]")
Split string with dot as delimiter
split()
accepts a regular expression, so you need to escape .
to not consider it as a regex meta character. Here's an example :
String[] fn = filename.split("\\.");
return fn[0];
spliting a string by space and dot and comma at the same time
The method split can be used with a Regex pattern, so you can match more elaborated cases to split your string.
A matching pattern for your case would be:
[ \.,]+
Regex Exaplanation:
[ .,]+ - The brackets create Character Set, that will match any character in the set.
[ .,]+ - The plus sign is a Quantifier, it will match the previous token (the character set) one or more times, this solves the problem where the tokens are following one another, creating empty strings in the array.
You can test it with the following code:
class Main {
public static void main(String[] args) {
String str = "Hello, World!, StackOverflow. Test Regex";
String[] split = str.split("[ .,]+");
for(String s : split){
System.out.println(s);
}
}
}
The output is:
Hello
World!
StackOverflow
Test
Regex
How to split a string on a 'dot' if, and only if, the preceding and following char is not a 'dot'
This regex would work:
s.split("(?<!\\.)\\.(?!\\.)");
The idea is to use negative lookahead to only split at "."
which is not followed or preceded by another dot.
How can I split by the last point of a String?
Use myString.lastIndexOf(".")
to get the index of the last dot.
For example, if you are sure that your input String contains at least one dot :
String name = myString.substring(0,myString.lastIndexOf("."));
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("\\.")
.
Related Topics
Places Where Javabeans Are Used
How to Convert Java String into Byte[]
Why Is Hibernate Open Session in View Considered a Bad Practice
Spring Boot and Multiple External Configuration Files
How to Specify the Java Compiler Version in a Pom.Xml File
How to Programmatically Download a Webpage in Java
Why Should a Java Class Implement Comparable
Using Regex to Generate Strings Rather Than Match Them
Regex for Matching Something If It Is Not Preceded by Something Else
How to Print a Float with 2 Decimal Places in Java
String's Maximum Length in Java - Calling Length() Method
Initialising a Multidimensional Array in Java
Replace Implicit Wait with Explicit Wait (Selenium Webdriver & Java)
Java.Sql.Sqlexception: - Ora-01000: Maximum Open Cursors Exceeded