Comment diviser une chaîne sur une séquence de caractères fixe?
Question
Supposons que j'ai la chaîne suivante:
String asd = "this is test ass this is test"
et je souhaite scinder la chaîne à l'aide de "ass". séquence de caractères.
j'ai utilisé:
asd.split("ass");
Ça ne marche pas. Que dois-je faire?
La solution
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);
}
}
}
Impressions:
this is test
this is test
Sous Java 6. Quelle sortie attendiez-vous?
Autres conseils
Cela semble bien fonctionner pour moi:
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 + "'");
}
}
}
Résultat:
'this is test '
' this is test'
Votre vrai délimiteur est-il différent peut-être? N'oubliez pas que split utilise son paramètre comme expression régulière ...
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
Essayez ça fonctionnera
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow