Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Convertir une chaine de caracteres en tableau

3 réponses
Avatar
cibox
Bonjour,

Question de d=E9butant.

J'ai une cha=EEne toto =3D "20\n10\n45.23\nazerty\n2006\n....\n"

je voudrais injecter la chaine toto ayant comme s=E9parateur le "\n"
dans un tableau de cha=EEne comme ci-dessous:

String resultat [0] =3D =AB 20 =BB;
String resultat [1] =3D =AB 10 =BB;
String resultat [2] =3D =AB 45.23=BB;
String resultat [3] =3D =AB azerty =BB;
String resultat [4] =3D =AB 2006 =BB;
String resultat [...] =3D =AB .. =BB;

Est-ce possible ?

3 réponses

Avatar
cfranco
wrote:

Question de débutant.

J'ai une chaîne toto = "20n10n45.23nazertyn2006n....n"

je voudrais injecter la chaine toto ayant comme séparateur le "n"
dans un tableau de chaîne comme ci-dessous:

String resultat [0] = « 20 »;
String resultat [1] = « 10 »;
String resultat [2] = « 45.23»;
String resultat [3] = « azerty »;
String resultat [4] = « 2006 »;
String resultat [...] = « .. »;

Est-ce possible ?


Oui, regarde la méthode split de la classe Pattern :

<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#sp
lit(java.lang.CharSequence)>

--
Christophe Franco

Avatar
Johann Burkard
wrote:
J'ai une chaîne toto = "20n10n45.23nazertyn2006n....n"

je voudrais injecter la chaine toto ayant comme séparateur le "n"
dans un tableau de chaîne comme ci-dessous:

String resultat [0] = « 20 »;
String resultat [1] = « 10 »;
String resultat [2] = « 45.23»;
String resultat [3] = « azerty »;
String resultat [4] = « 2006 »;
String resultat [...] = « .. »;


StringTokenizer tk = new StringTokenizer(toto, "n");
String[] out = new String[tk.countTokens()];
int i = 0;
while (tk.hasNext()) {
out[i++] = tk.nextToken();
}

Johann
--
Usenet ist lediglich ein Transportsystem fuer Texte. Niemand hat mehr
Rechte wie der andere, denn darauf gruendete sich Usenet. Das kann man
aber auch in der NQ nachlesen.
(*Tönnes in <actdqo$cpv$)

Avatar
Christophe
Christophe Franco wrote:
wrote:

Question de débutant.

J'ai une chaîne toto = "20n10n45.23nazertyn2006n....n"

je voudrais injecter la chaine toto ayant comme séparateur le "n"
dans un tableau de chaîne comme ci-dessous:

String resultat [0] = « 20 »;
String resultat [1] = « 10 »;
String resultat [2] = « 45.23»;
String resultat [3] = « azerty »;
String resultat [4] = « 2006 »;
String resultat [...] = « .. »;

Est-ce possible ?


Oui, regarde la méthode split de la classe Pattern :

<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#sp
lit(java.lang.CharSequence)>



Celle de la classe string devrait suffir ;-)
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

String[] resultat = toto.split("n");