次の文字列があるとします。
String asd = "this is test ass this is test"
「お尻」文字列を使用して文字列を分割したい。
私が使用した:
asd.split("ass");
うまくいきません。私は何をする必要がありますか?
私にとってはうまくいくようです:
public class Test
{
public static void main(String[] args) {
String asd = "this is test ass this is test";
String[] bits = asd.split("ass");
for (String bit : bits) {
System.out.println("'" + bit + "'");
}
}
}
結果:
'this is test '
' this is test'
あなたの本当の区切り文字はおそらく異なりますか?split はそのパラメーターを正規表現として使用することを忘れないでください...
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
これを試してみてください
public class Splitter {
public static void main(final String[] args) {
final String asd = "this is test ass this is test";
final String[] parts = asd.split("ass");
for (final String part : parts) {
System.out.println(part);
}
}
}
版画:
this is test
this is test
Java 6 の下で。どのような出力を期待していましたか?