Chaîne Java lastIndexOf ()

La méthode String lastIndexOf () renvoie l'index de la dernière occurrence du caractère / sous-chaîne spécifié dans la chaîne.

La syntaxe de la lastIndexOf()méthode String soit

 string.lastIndexOf(int ch, int index)

ou

 string.lastIndexOf(string str, int index)

Paramètres lastIndexOf ()

Pour trouver le dernier index d'un caractère, lastIndexOf()prend ces deux paramètres:

  • ch - le caractère dont le dernier index doit être trouvé
  • index (facultatif) - s'il indexest passé, le chcaractère est recherché du début à cet index

Pour trouver le dernier index de la sous-chaîne spécifiée dans la chaîne, lastIndexOf()prend ces deux paramètres:

  • str - la chaîne dont le dernier index doit être trouvé
  • index (facultatif) - s'il indexest passé, la strchaîne est recherchée du début à cet index

lastIndexOf () Valeur renvoyée

  • renvoie l'index de la dernière occurrence du caractère / chaîne spécifié
  • renvoie -1 si le caractère / la chaîne spécifié est introuvable.

Exemple 1: chaîne Java lastIndexOf ()

 // Java String lastIndexOf() with only one parameter class Main ( public static void main(String() args) ( String str1 = "Learn Java"; int result; // getting index of character 'J' result = str1.lastIndexOf('J'); System.out.println(result); // 6 // the last occurrence of 'a' is returned result = str1.lastIndexOf('a'); System.out.println(result); // 9 // character not in the string result = str1.lastIndexOf('j'); System.out.println(result); // -1 // getting the last occurrence of "ava" result = str1.lastIndexOf("ava"); System.out.println(result); // 7 // substring not in the string result = str1.lastIndexOf("java"); System.out.println(result); // -1 ) )

Remarque: le caractère 'a'apparaît plusieurs fois dans la "Learn Java"chaîne. La lastIndexOf()méthode renvoie l'index de la dernière occurrence de 'a'(qui est 9).

Exemple 2: lastIndexOf () avec le paramètre fromIndex

 class Main ( public static void main(String() args) ( String str1 = "Learn Java programming"; int result; // search from index 0 to 4 // searches the substring "Learn" result = str1.lastIndexOf('r', 4); System.out.println(result); // 3 // search from index 0 to 12 // searcheses the substring "Learn Java pr" result = str1.lastIndexOf('r', 12); System.out.println(result); // 12 // string length is less than 100 // searches the whole string result = str1.lastIndexOf('r', 70); System.out.println(result); // 15 // searches the substring "Learn" result = str1.lastIndexOf("Java", 4); System.out.println(result); // -1 ) )

Remarques:

  • La dernière occurrence de 'r'dans la "Learn Java programming"chaîne est à l'index 15. Cependant, str1.lastIndexOf('r', 4)recherche la sous-chaîne "Learn". Le dernier index de 'r'in "Learn"est à l'index 3.
  • str1.lastIndexOf('r', 12)recherche la sous-chaîne "Learn Java pr". Le dernier index de 'r'in "Learn Java pr"est à l'index 12.
  • str1.lastIndexOf("Java", 4)recherche la sous-chaîne "Learn". Puisqu'il n'y a aucun "Java"dans la "Learn"sous - chaîne, le résultat est -1.

Lecture recommandée: Java String indexOf ()

Articles intéressants...