1

I'm experimenting with String class's instance method split(). However, when I use . as split() method's argument and trying to print it out using Arrays.toString(s2), only an empty array gets printed out.

Why is this happening?

String s1 = "hello.world";
String[] s2 = s1.split(".");
System.out.println(Arrays.toString(s2));
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137

5 Answers5

4

This " weird" behavior is because you are not scaping the dot in the regex... i.e you need to do s1.split("\\."); and not s1.split(".");

Example:

public static void main(String[] args) {
        String s1 = "hello.world";
        String[] s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));
}

just for the learning process the dot in the regex belongs to the known metacharacters and every time you try to use regexwith them they must be scaped...

take a look at this tutorial for more info...

there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

As per the API, the split method takes a regular expression as argument. It so happens that in regular expression syntax, the period means any character, thus you are splitting on every character, which is why you are getting the empty array.

To fix this, you will need to escape the period like so: .split("\\."). The extra \ instructs the regex engine to treat the period as an actual character, devoid of any special meaning.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

You can escape special characters using these methods

1.String test = "abc.xyz";
String[] output1 = test.split("\.");

2.String test = "abc.xyz";
String[] output2 = test.split(Pattern.quote("."));

You can refer to split a string

Community
  • 1
  • 1
js_248
  • 2,032
  • 4
  • 27
  • 38
1

. is a special char in regex and split accepts regex as param, you should escape it.

Areca
  • 1,292
  • 4
  • 11
  • 21
1

Try with,

 String[] s2 = s1.split("\\.");

There are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. In literal Java strings the backslash is an escape character. The literal string "\\" is a single backslash.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55