I've been writing Java for the first time in a while, and when I tried to separate strings with .
, I was lightly addicted to it.
Python looks like this.
hoge = '172.0.0.1'
fuga = hoge.split('.') # => ['172', '0', '0', '1']
In Java with the same feeling
String hoge = "172.0.0.1";
String[] fuga = hoge.split(".");
If you write, an array of length 0 will be returned.
The cause is that Java split takes a pattern string as an argument.
.
Matches any single character. (..) φ memo memo
String hoge = "172.0.0.1";
String[] fuga = hoge.split("7."); // => ["1", ".0.0.1"]
If you expect the same behavior as Python:
import java.util.regex.Pattern;
String hoge = "172.0.0.1";
String[] fuga = hoge.split(Pattern.quote("."));
Recommended Posts