I am wondering if I am going about splitting a string on a .
the right way? My code is:
String[] fn = filename.split("."); return fn[0];
I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that .
means any character, so now I'm stuck.
Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot.
String split example s = "Python string example. We split it using the dot character." parts = s. split(".")
To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.
The easiest way is to check with a . contains() statement.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With