String.IndexOfAny Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Informa del índice de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. El método devuelve -1 si los caracteres de la matriz no se encuentran en esta instancia.
Sobrecargas
| Nombre | Description |
|---|---|
| IndexOfAny(Char[]) |
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. |
| IndexOfAny(Char[], Int32) |
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. La búsqueda comienza en una posición de carácter especificada. |
| IndexOfAny(Char[], Int32, Int32) |
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. La búsqueda comienza en una posición de carácter especificada y examina un número especificado de posiciones de caracteres. |
IndexOfAny(Char[])
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode.
public:
int IndexOfAny(cli::array <char> ^ anyOf);
public int IndexOfAny(char[] anyOf);
member this.IndexOfAny : char[] -> int
Public Function IndexOfAny (anyOf As Char()) As Integer
Parámetros
- anyOf
- Char[]
Matriz de caracteres Unicode que contiene uno o varios caracteres que se van a buscar.
Devoluciones
Posición de índice de base cero de la primera aparición en esta instancia en la que se encontró cualquier carácteranyOf; -1 si no se encontró ningún carácter.anyOf
Excepciones
anyOf es null.
Ejemplos
En el ejemplo siguiente se busca la primera vocal en una cadena.
char[] chars = { 'a', 'e', 'i', 'o', 'u', 'y',
'A', 'E', 'I', 'O', 'U', 'Y' };
String s = "The long and winding road...";
Console.WriteLine($"""
The first vowel in
'{s}'
is found at index {s.IndexOfAny(chars)}
""");
// The example displays the following output:
// The first vowel in
// 'The long and winding road...'
// is found at index 2
let chars = [| 'a'; 'e'; 'i'; 'o'; 'u'; 'y'
'A'; 'E'; 'I'; 'O'; 'U'; 'Y' |]
let s = "The long and winding road..."
printfn $"The first vowel in \n {s}\nis found at index {s.IndexOfAny chars}"
// The example displays the following output:
// The first vowel in
// The long and winding road...
// is found at index 2
Module Example1
Public Sub Run()
Dim chars() As Char = { "a"c, "e"c, "i"c, "o"c, "u"c, "y"c,
"A"c, "E"c, "I"c, "O"c, "U"c, "Y"c }
Dim s As String = "The long and winding road..."
Console.WriteLine("The first vowel in {2} {0}{2}is found at index {1}",
s, s.IndexOfAny(chars), vbCrLf)
End Sub
End Module
' The example displays the following output:
' The first vowel in
' The long and winding road...
' is found at index 2
Comentarios
La numeración de índice comienza desde cero.
La búsqueda de anyOf distingue mayúsculas de minúsculas. Si anyOf es una matriz vacía, el método devuelve -1.
Este método realiza una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si sus valores escalares Unicode son los mismos. Para realizar una búsqueda sensible a la referencia cultural, use el CompareInfo.IndexOf método , donde un valor escalar Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural.
Consulte también
Se aplica a
IndexOfAny(Char[], Int32)
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. La búsqueda comienza en una posición de carácter especificada.
public:
int IndexOfAny(cli::array <char> ^ anyOf, int startIndex);
public int IndexOfAny(char[] anyOf, int startIndex);
member this.IndexOfAny : char[] * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer) As Integer
Parámetros
- anyOf
- Char[]
Matriz de caracteres Unicode que contiene uno o varios caracteres que se van a buscar.
- startIndex
- Int32
Posición inicial de búsqueda.
Devoluciones
Posición de índice de base cero de la primera aparición en esta instancia en la que se encontró cualquier carácteranyOf; -1 si no se encontró ningún carácter.anyOf
Excepciones
anyOf es null.
startIndex es negativo.
O bien
startIndex es mayor que el número de caracteres de esta instancia.
Ejemplos
En el ejemplo siguiente se busca el índice de la aparición de cualquier carácter de la cadena "is" dentro de una subcadena de otra cadena.
string br1 = "0----+----1----+----2----+----3" +
"----+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
string str = "Now is the time for all good men " +
"to come to the aid of their party.";
int start;
int at;
string target = "is";
char[] anyOf = target.ToCharArray();
start = str.Length / 2;
Console.WriteLine();
Console.WriteLine("The first character occurrence " +
$"from position {start} to {str.Length - 1}:");
Console.WriteLine($"""
{Environment.NewLine}{br1}{Environment.NewLine}
{br2}{Environment.NewLine}{str}{Environment.NewLine}
""");
Console.Write($"A character in '{target}' occurs at position: ");
at = str.IndexOfAny(anyOf, start);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();
/*
The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
A character in 'is' occurs at position: 49
*/
// Sample for String.IndexOfAny(Char[], Int32)
open System
let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "is"
let anyOf = target.ToCharArray()
let start = str.Length/2
printfn $"\nThe first character occurrence from position {start} to {str.Length - 1}."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "
let at = str.IndexOfAny(anyOf, start)
if at > -1 then
printfn $"{at}"
else
printfn "(not found)"
(*
The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
A character in 'is' occurs at position: 49
*)
' Sample for String.IndexOfAny(Char[], Int32)
Class Example2
Public Shared Sub Run()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim target As String = "is"
Dim anyOf As Char() = target.ToCharArray()
start = str.Length / 2
Console.WriteLine()
Console.WriteLine("Search for a character occurrence from position {0} to {1}.", _
start, str.Length - 1)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("A character in '{0}' occurs at position: ", target)
at = str.IndexOfAny(anyOf, start)
If at > - 1 Then
Console.Write(at)
Else
Console.Write("(not found)")
End If
Console.WriteLine()
End Sub
End Class
'
'
'Search for a character occurrence from position 33 to 66.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'A character in 'is' occurs at position: 49
'
Comentarios
La numeración de índice comienza desde cero. El startIndex parámetro puede oscilar entre 0 y uno menor que la longitud de la instancia de cadena.
La búsqueda va de startIndex hasta el final de la cadena.
La búsqueda de anyOf distingue mayúsculas de minúsculas.
Este método realiza una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si su valor escalar Unicode es el mismo. Para realizar una búsqueda sensible a la referencia cultural, use el CompareInfo.IndexOf método , donde un valor escalar Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural.
Consulte también
Se aplica a
IndexOfAny(Char[], Int32, Int32)
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
- Source:
- String.Searching.cs
Informa del índice de base cero de la primera aparición en esta instancia de cualquier carácter de una matriz especificada de caracteres Unicode. La búsqueda comienza en una posición de carácter especificada y examina un número especificado de posiciones de caracteres.
public:
int IndexOfAny(cli::array <char> ^ anyOf, int startIndex, int count);
public int IndexOfAny(char[] anyOf, int startIndex, int count);
member this.IndexOfAny : char[] * int * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer, count As Integer) As Integer
Parámetros
- anyOf
- Char[]
Matriz de caracteres Unicode que contiene uno o varios caracteres que se van a buscar.
- startIndex
- Int32
Posición inicial de búsqueda.
- count
- Int32
Número de posiciones de caracteres que se van a examinar.
Devoluciones
Posición de índice de base cero de la primera aparición en esta instancia en la que se encontró cualquier carácteranyOf; -1 si no se encontró ningún carácter.anyOf
Excepciones
anyOf es null.
count o startIndex es negativo.
O bien
count
+
startIndex es mayor que el número de caracteres de esta instancia.
Ejemplos
En el ejemplo siguiente se busca el índice de la aparición de cualquier carácter de la cadena "aid" dentro de una subcadena de otra cadena.
string br1 = "0----+----1----+----2----+----3----" +
"+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
string str = "Now is the time for all good men " +
"to come to the aid of their party.";
string target = "aid";
char[] anyOf = target.ToCharArray();
int start = (str.Length - 1) / 3;
int count = (str.Length - 1) / 4;
Console.WriteLine();
Console.WriteLine("The first character occurrence from " +
$"position {start} for {count} characters:");
Console.WriteLine($"""
{Environment.NewLine}{br1}{Environment.NewLine}{br2}
{Environment.NewLine}{str}{Environment.NewLine}
""");
Console.Write($"A character in '{target}' occurs at position: ");
int at = str.IndexOfAny(anyOf, start, count);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();
/*
The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
A character in 'aid' occurs at position: 27
*/
// Sample for String.IndexOfAny(Char[], Int32, Int32)
open System
let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "aid"
let anyOf = target.ToCharArray()
let start = (str.Length - 1) / 3
let count = (str.Length - 1) / 4
printfn $"\nThe first character occurrence from position {start} for {count} characters."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "
let at = str.IndexOfAny(anyOf, start, count)
if at > -1 then
printfn $"{at}"
else
printfn "(not found)"
(*
The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
A character in 'aid' occurs at position: 27
*)
' Sample for String.IndexOfAny(Char[], Int32, Int32)
Class Example3
Public Shared Sub Run()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim count As Integer
Dim target As String = "aid"
Dim anyOf As Char() = target.ToCharArray()
start =(str.Length - 1) / 3
count =(str.Length - 1) / 4
Console.WriteLine()
Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("A character in '{0}' occurs at position: ", target)
at = str.IndexOfAny(anyOf, start, count)
If at > - 1 Then
Console.Write(at)
Else
Console.Write("(not found)")
End If
Console.WriteLine()
End Sub
End Class
'
'The first character occurrence from position 22 for 16 characters.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'A character in 'aid' occurs at position: 27
'
Comentarios
La búsqueda comienza en startIndex y continúa hasta startIndex + count -1. El carácter en startIndex + count no se incluye en la búsqueda.
La numeración de índice comienza desde cero. El startIndex parámetro puede oscilar entre 0 y uno menor que la longitud de la instancia de cadena.
La búsqueda de anyOf distingue mayúsculas de minúsculas.
Este método realiza una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si su valor escalar Unicode es el mismo. Para realizar una búsqueda sensible a la referencia cultural, use el CompareInfo.IndexOf método , donde un valor escalar Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural.