Zichen Ma
Zichen Ma

Reputation: 987

How java split() exactly works?

why using split() in java works differently? I want to split a version string like this: 1.2.3.4 however if I do like this: will get an empty array,, if I use split("\\."), it works as my expected:

        String version1 = "1.2.3.4.5";
        String version2 = "1.2.3.4.5.6";
        String[] v1Arr = version1.split("."); 
        String[] v2Arr = version2.split("\\."); 
        System.out.println(Arrays.toString(v1Arr)); // [] why?
        System.out.println(Arrays.toString(v2Arr)); // [1, 2, 3, 4, 5, 6]


String version1 = "1-2-3-4-5";
String version2 = "1-2-3-4-5-6";
String[] v1Arr = version1.split("-");
String[] v2Arr = version2.split("\\-");

System.out.println(Arrays.toString(v1Arr)); // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(v2Arr)); // [1, 2, 3, 4, 5, 6]

If I change "." to "-" both work as expected, why does this happens? Thank you in advance!

Upvotes: 2

Views: 384

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521279

Just as an alternative to the @Elliott Frisch answer, you may also split on a character or characters contained within a regex character class. Consider the following code snippet:

String version = "1.2.3.4.5.6";
String[] parts = version.split("[.]");

The square brackets tell the regex engine to treat whatever is inside as a literal character, in this case, a literal dot. This approach can be even more useful when trying to split on several characters, many of which are perhaps meta characters. Using bracket notation, we don't have to worry about special escaping syntax.

Upvotes: 2

Ami Patel
Ami Patel

Reputation: 31

\ is called escape character and . dot interpreted as escape character so you are not able to use dot directly to split an string

that’s why you have to use \ as escape character with dot , to represent as dot in string

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

. is a special pattern token in a regular expression. It matches any one character. When you split on every possible character you get an empty array (because there is nothing left). In contrast, when you escape the . with \\. the token is rendered as a literal (and only matches a literal .).

Upvotes: 6

Related Questions