1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

正規表現を使わないString#split

Last updated at Posted at 2015-06-30

今日(もう昨日)Apache Commonsのない環境で
文字列を文字列で分割(正規表現使わない)するのてどうやるんだっけ?って思い出せなくて、
やったことなかったことに気が付いたので記事書きます。

(Apache Commons使うならStringUtils#splitByWholeSeparatorでしたっけ。)

		{
			String s = "A,B,C";
			String d = ",";
			System.out.println(s + "を" + d + "でsplit");
			System.out.println(Arrays.toString(s.split(d, -1)));
			System.out.println(Arrays.toString(s.split(Pattern.quote(d), -1)));
		}

		{
			String s = "A.B.C";
			String d = ".";
			System.out.println(s + "を" + d + "でsplit");
			System.out.println(Arrays.toString(s.split(d, -1)));
			System.out.println(Arrays.toString(s.split(Pattern.quote(d), -1)));
		}

結果

A,B,Cを,でsplit
[A, B, C]
[A, B, C]
A.B.Cを.でsplit
[, , , , , ]
[A, B, C]

Pattern#quote使うだけって話。
ひしだまさんも言ってます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?