este problema se puede resolver de variadas maneras, pero te mostramos la respuesta más completa para nosotros.
Ejemplo 1: c índice agudo de subcadena
// To find the index of the first substring in a string use// 'IndexOf()'string str ="Hello World!"
str.IndexOf("o");// Output: 4// You can give a starting index and a length to search at
str.IndexOf("o",6,4);// Output: 7// To find the last instance of a substring use 'LastIndexOf()'
str.LastIndexOf("o");// Output: 7
Ejemplo 2: c # IndexOf
string word ="Hello";intwhere= word.IndexOf("e");// int where returns index number where the letter "e" is located.// --0-1-2-3-4-- < Index number// --H-e-l-l-o-- < Word// In this case, the letter "e" is located in the index number 1
Ejemplo 3: índice de string C#
string br1 ="0----+----1----+----2----+----3----+----4----+----5----+----6----+---";string br2 ="012345678901234567890123456789012345678901234567890123456789012345678";string str ="Now is the time for all good men to come to the aid of their country.";int start;int at;int end;int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position 0 to 1.", start, end-1);
Console.WriteLine("102030", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count =0;
at =0;while((start <= end)&&(at >-1))// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);if(at ==-1)break;
Console.Write("0 ", at);
start = at+1;
Console.WriteLine();/*
This example produces the following results:
All occurrences of 'he' from position 34 to 68.
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
012345678901234567890123456789012345678901234567890123456789012345678
Now is the time for all good men to come to the aid of their country.
The string 'he' occurs at position(s): 45 56
*/
Ejemplo 4: strinng.indexOf c #
int index = String.IndexOf(char x);int index = String.IndexOf(string x);
Reseñas y puntuaciones
Si aceptas, puedes dejar un escrito acerca de qué le añadirías a este enunciado.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)