You can split a string by dot in Java using the
split() method and one of two regular expressions, either
Being able to splice Java strings around dots using a regex is highly useful, given every computer or device on the planet has a string IP address denoted by numbers separated by dots, and files with dot separated file extensions. Indeed, knowing this will allow you to parse any Java string, whether an IP address or not, into individual string tokens (words) as a Java String array.
Table of Contents
Splitting strings by dot
To split a Java string by dot you need to use the Java String class Java String
split() method and its regex method version. You cannot use the Java String
replace() method or
Java String substring() without elaborated and unnecessary code.
Note, the regex
Java String split() method will throw a
PatternSyntaxException if the regular expression’s syntax is invalid. While it returns an array of Java String objects (
This is slightly counter-intuitive as you’re initial intuition will likely be to just pass in “.” into the
split() method as a parameter. This is the approach we followed in the article Java Split String by White Space for white space or tab characters.
Dots as delimiter
However, even though IP addresses are everywhere, dots or periods are not seen as a common separator or delimiter in string handling in Java. This is why a regex has to be used. But it is further complicated by the fact that denoting a dot in a regex is not particularly straightforward either. It is seen as a special character requiring its own notation.
The actual notation is similar to escaping a new line or tab character in Java, but this time two backslashes are needed instead. The dot regex is thus:
"\\.". There is also a second regex that can be used and
Splitting a String by dot using regular expressions
As discussed, the intuitive way to split a string by period cannot be used. If we were to try and “attempt” it however on a filename, we would use the following code.
String str = "myfile.csv" ; String file = str.split(".") ; String ext = str.split(".") ;
The dot will not be recognized though when the Java String
split() method is called this way, meaning that a zero length string array will be returned. This itself will cause an out of bounds array exception. The correct ways and Java are as follows.
"\\." as a parameter
The parameter being passed needs two backslashes instead or enclosed in square brackets to make the previous Java work. The following code in this step will work, as we have now used two backslashes as required.
String ext = "myfile.txt".split("\\.");
Notice, how this Java will cause the last item of the array to be returned and stored in the string variable.
split() with a regex
Similarly, this next line of code will also work as we have enclosed the period this time inside of two square brackets to form a regex instead of using two backslashes.
String ext = "myfile.txt".split("[.]");
Splitting a string by multiple dots
You may also want to split a string which contains multiple dots in Java too. This can be simply accomplished by including an extra escaped dot in the regex as the follows code shows.
String ext = "myfile..txt".split("\\.\\");
In this code the
\\.\\. regex will match both the dots in the filename, rather than just one; as before an array of strings will be returned by the method.
Array Index Out Of Bounds Exception exceptions
Often when splitting a string by dot you may get an
ArrayIndexOutOfBoundsException. This is because there is no valid regex with a dot to split the calling string by, so the returned array is empty. Indeed, the
split() method returns an empty array by default since it removes all empty strings (blanks) from the result.
The solution is to use the Java
split(regex, limit) method instead. If you set
-1 empty strings will be returned in the string array too as follows.
".".split("\\.", -1) // gives ["", ""]
To recap: when the calling string is just a dot
".".split("\\.", -1) will return a blank, but calling
".".split("\\.") will throw an
This article has discussed how to split a string by dot in Java and walked you through the theory and a number of different examples. The key point to remember is you need to form the regex correctly, rather than just use a dot in quotes; you should use the alternative regex split method with a limit parameter if appropriate.
Try reading a list of IP addresses from a file as an exercise. You can use what you have learnt in this article to parse each line by dot to get the individual integers making up the IPs. Alternatively, you could also get the local IP address in Java of a computer and split that IP by dot too.