I’m trying to split a line of text into multiple parts. Each element of the text is separated by a period.
I’m using string.split(“.”); to split the text into an array of Strings, but not getting anywhere.
Here is a sample of the code:
String fileName = "testing.one.two";
String[] fileNameSplit = fileName.split(".");
System.out.println(fileNameSplit[0]);
The funny thing is, when I try “:” instead of “.”, it works? How can I get it to work for a period?
String.split()accepts a regular expression (regex for short) and dot is a special char in regexes. It means “match all chars except newlines”. So you must escape it with a leading backslash. But the leading backslash is a special character in java string literals. It denotes an escape sequence. So it must be escaped too, with another leading backslash. Like this: