Indexof

cs_amel2006 Messages postés 83 Date d'inscription samedi 21 janvier 2006 Statut Membre Dernière intervention 9 décembre 2006 - 10 avril 2006 à 20:23
etudev Messages postés 47 Date d'inscription vendredi 10 février 2006 Statut Membre Dernière intervention 23 mai 2006 - 10 avril 2006 à 20:40
voici mon code:


Code:
,
----

String f="resTitle"; int i=0;
Pattern pattern = Pattern.compile("resTitle");
Matcher matcher = pattern.matcher(g);
while(matcher.find())
{
i=g.indexOf(f,i);
System.out.println(i);
}

il m'affiche toujour le premier emplacement de ce mot pourtant il existe plusieur emplacement


amel2006

1 réponse

etudev Messages postés 47 Date d'inscription vendredi 10 février 2006 Statut Membre Dernière intervention 23 mai 2006
10 avril 2006 à 20:40
Bonjour,
indexOf public int indexOf(String str, int fromIndex) : Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Tu peux faire une boucle :
1- recuperer l'index du premier restitle.
2- utiliser la methode suivante:
public String substring(int beginIndex):Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. Examples: "unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)

3-refaire 1 et 2 jusqu'a la fin de g.

Bonne soirée
0
Rejoignez-nous