web-dev-qa-db-fra.com

Android Fractionner la chaîne

J'ai une chaîne appelée CurrentString et est sous la forme de quelque chose comme ceci "Fruit: they taste good".
J'aimerais scinder la CurrentString en utilisant le : comme délimiteur.
Ainsi, le mot "Fruit" sera divisé en sa propre chaîne et "they taste good" sera une autre chaîne.
Ensuite, je voudrais simplement utiliser SetText() sur 2 différentes TextViews pour afficher cette chaîne.

Quelle serait la meilleure façon d'aborder cela?

209
zaid
String currentString = "Fruit: they taste good";
String[] separated = currentString.split(":");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

Vous voudrez peut-être supprimer l'espace de la deuxième chaîne:

separated[1] = separated[1].trim();

Si vous souhaitez scinder la chaîne avec un caractère spécial tel que point (.), Vous devez utiliser le caractère d'échappement\avant le point.

Exemple:

String currentString = "Fruit: they taste good.very Nice actually";
String[] separated = currentString.split("\\.");
separated[0]; // this will contain "Fruit: they taste good"
separated[1]; // this will contain "very Nice actually"

Il y a d'autres façons de le faire. Par exemple, vous pouvez utiliser la classe StringTokenizer (à partir de Java.util):

StringTokenizer tokens = new StringTokenizer(currentString, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();// this will contain " they taste good"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method
574
Cristian

La méthode .split fonctionnera, mais elle utilise des expressions régulières. Dans cet exemple, il s'agirait de (voler à Cristian):

String[] separated = CurrentString.split("\\:");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

En outre, cela venait de: le split Android ne fonctionne pas correctement

83
Silas Greenback

Chaîne fendue Android par virgule

String data = "1,Diego Maradona,Footballer,Argentina";
String[] items = data.split(",");
for (String item : items)
{
    System.out.println("item = " + item);
}
47
mahasam
     String s = "having Community Portal|Help Desk|Local Embassy|Reference Desk|Site News";
     StringTokenizer st = new StringTokenizer(s, "|");
        String community = st.nextToken();
        String helpDesk = st.nextToken(); 
        String localEmbassy = st.nextToken();
        String referenceDesk = st.nextToken();
        String siteNews = st.nextToken();
22
Faakhir

Vous pouvez également envisager la méthode spécifique de Android TextUtils.split () .

La différence entre TextUtils.split () et String.split () est documentée avec TextUtils.split ():

String.split () renvoie [''] lorsque la chaîne à scinder est vide. Ceci retourne []. Cela ne supprime aucune chaîne vide du résultat.

Je trouve cela un comportement plus naturel. En substance, TextUtils.split () est juste un wrapper fin pour String.split (), traitant spécifiquement du cas des chaînes vides. Le code de la méthode est en fait assez simple.

22
gardarh