|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
String.LastIndexOf, méthode (String, Int32)
Espace de noms : System
Assembly : mscorlib (dans mscorlib.dll)
Paramètres
- value
- Type : System.String
Chaîne à rechercher.
- startIndex
- Type : System.Int32
Position de départ de la recherche. La recherche se poursuit à partir de startIndex vers le début de cette instance.
Valeur de retour
Type : System.Int32| Exception | Condition |
|---|---|
| ArgumentNullException | |
| ArgumentOutOfRangeException |
using System; public class Example { public static void Main() { int position = 0; string s1 = "ani\u00ADmal"; string s2 = "animal"; // Find the index of the soft hyphen. position = s1.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s1.LastIndexOf("\u00AD", position)); position = s2.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s2.LastIndexOf("\u00AD", position)); // Find the index of the soft hyphen followed by "n". position = s1.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s1.LastIndexOf("\u00ADn", position)); position = s2.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s2.LastIndexOf("\u00ADn", position)); // Find the index of the soft hyphen followed by "m". position = s1.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s1.LastIndexOf("\u00ADm", position)); position = s2.LastIndexOf("m"); Console.WriteLine("'m' at position {0}", position); if (position >= 0) Console.WriteLine(s2.LastIndexOf("\u00ADm", position)); } } // The example displays the following output: // 'm' at position 4 // 4 // 'm' at position 3 // 3 // 'm' at position 4 // 1 // 'm' at position 3 // 1 // 'm' at position 4 // 4 // 'm' at position 3 // 3
// Sample for String.LastIndexOf(String, Int32) using System; class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; int start; int at; start = str.Length-1; Console.WriteLine("All occurrences of 'he' from position {0} to 0.", start); Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str); Console.Write("The string 'he' occurs at position(s): "); at = 0; while((start > -1) && (at > -1)) { at = str.LastIndexOf("he", start); if (at > -1) { Console.Write("{0} ", at); start = at - 1; } } Console.Write("{0}{0}{0}", Environment.NewLine); } } /* This example produces the following results: All occurrences of 'he' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 56 45 8 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.