String.IndexOfAny String.IndexOfAny String.IndexOfAny String.IndexOfAny Method

Definición

Devuelve el índice de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters. El método devuelve -1 si los caracteres de la matriz no se encuentran en esta instancia.The method returns -1 if the characters in the array are not found in this instance.

Sobrecargas

IndexOfAny(Char[]) IndexOfAny(Char[]) IndexOfAny(Char[]) IndexOfAny(Char[])

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.

IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32)

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. La búsqueda comienza en una posición de carácter especificada.The search starts at a specified character position.

IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32)

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. La búsqueda comienza en una posición de carácter especificada y examina un número especificado de posiciones de caracteres.The search starts at a specified character position and examines a specified number of character positions.

IndexOfAny(Char[]) IndexOfAny(Char[]) IndexOfAny(Char[]) IndexOfAny(Char[])

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.

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 más caracteres que se van a buscar.A Unicode character array containing one or more characters to seek.

Devoluciones

Posición de índice de base cero de la primera aparición en la instancia en cuestión donde se encontró cualquier carácter de anyOf; -1 si no se encontró ningún carácter de anyOf.The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

Excepciones

Ejemplos

El ejemplo siguiente busca la primera vocal en una cadena.The following example finds the first vowel in a string.

using System;

public class Example
{
   public static void Main()
   {
      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 \n   {0}\nis found at position {1}", 
                        s, s.IndexOfAny(chars) + 1);                         
   }
}
// The example displays the following output:
//       The first vowel in
//          The long and winding road...
//       is found at position 3
Module Example
   Public Sub Main()
      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 position {1}", 
                        s, s.IndexOfAny(chars) + 1, vbCrLf)                         
   End Sub
End Module
' The example displays the following output:
'       The first vowel in
'          The long and winding road...
'       is found at position 3

Comentarios

Índice de la numeración empieza desde cero.Index numbering starts from zero.

La búsqueda de anyOf distingue mayúsculas de minúsculas.The search for anyOf is case-sensitive. Si anyOf es una matriz vacía, la encuentra método una coincidencia al principio de la cadena (es decir, en el índice cero).If anyOf is an empty array, the method finds a match at the beginning of the string (that is, at index zero).

Este método realiza una búsqueda ordinal (sin tener en cuenta la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores escalares Unicode son iguales.This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. Para realizar una búsqueda de referencias culturales, utilice el CompareInfo.IndexOf método, donde un valor escalar Unicode que representa un carácter compuesto, como la ligadura "Æ" U + 00 C 6, es posible que se considera equivalente a cualquier aparición de los componentes del carácter en el Corrija la secuencia, como "AE" (u+0041, u+0045), en función de la referencia cultural.To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

Consulte también:

IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32) IndexOfAny(Char[], Int32)

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. La búsqueda comienza en una posición de carácter especificada.The search starts at a specified character position.

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 más caracteres que se van a buscar.A Unicode character array containing one or more characters to seek.

startIndex
Int32 Int32 Int32 Int32

Posición en la que comienza la búsqueda.The search starting position.

Devoluciones

Posición de índice de base cero de la primera aparición en la instancia en cuestión donde se encontró cualquier carácter de anyOf; -1 si no se encontró ningún carácter de anyOf.The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

Excepciones

startIndex es negativo.startIndex is negative.

O bien-or- startIndex es mayor que el número de caracteres de esta instancia.startIndex is greater than the number of characters in this instance.

Ejemplos

El ejemplo siguiente se busca el índice de la aparición de cualquier carácter de la cadena "es" dentro de una subcadena de otra cadena.The following example finds the index of the occurrence of any character of the string "is" within a substring of another string.

// Sample for String::IndexOfAny(Char[], Int32)
using namespace System;
int 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;
   String^ target = "is";
   array<Char>^anyOf = target->ToCharArray();
   start = str->Length / 2;
   Console::WriteLine();
   Console::WriteLine( "The first 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 )
      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)
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;
    string target = "is";
    char[] anyOf = target.ToCharArray();

    start = str.Length/2;
    Console.WriteLine();
    Console.WriteLine("The first 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) 
        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)
Imports System

Class Sample
   Public Shared Sub Main()
      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 'Main
End Class 'Sample
'
'
'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

Índice de la numeración empieza desde cero.Index numbering starts from zero. El startIndex parámetro puede oscilar entre 0 y uno menos que la longitud de la instancia de cadena.The startIndex parameter can range from 0 to one less than the length of the string instance.

La búsqueda comienza en startIndex al final de la cadena.The search ranges from startIndex to the end of the string.

La búsqueda de anyOf distingue mayúsculas de minúsculas.The search for anyOf is case-sensitive.

Este método realiza una búsqueda ordinal (sin tener en cuenta la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores escalares Unicode son iguales.This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar value are the same. Para realizar una búsqueda de referencias culturales, utilice el CompareInfo.IndexOf método, donde un valor escalar Unicode que representa un carácter compuesto, como la ligadura "Æ" U + 00 C 6, es posible que se considera equivalente a cualquier aparición de los componentes del carácter en el Corrija la secuencia, como "AE" (u+0041, u+0045), en función de la referencia cultural.To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

Consulte también:

IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32) IndexOfAny(Char[], Int32, Int32)

Devuelve el índice de base cero de la primera aparición en la instancia de un carácter de una matriz de caracteres Unicode especificada.Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. La búsqueda comienza en una posición de carácter especificada y examina un número especificado de posiciones de caracteres.The search starts at a specified character position and examines a specified number of character positions.

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 más caracteres que se van a buscar.A Unicode character array containing one or more characters to seek.

startIndex
Int32 Int32 Int32 Int32

Posición en la que comienza la búsqueda.The search starting position.

count
Int32 Int32 Int32 Int32

Número de posiciones de caracteres que se van a examinar.The number of character positions to examine.

Devoluciones

Posición de índice de base cero de la primera aparición en la instancia en cuestión donde se encontró cualquier carácter de anyOf; -1 si no se encontró ningún carácter de anyOf.The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

Excepciones

count o startIndex es negativo.count or startIndex is negative.

O bien-or- count + startIndex es mayor que el número de caracteres de esta instancia.count + startIndex is greater than the number of characters in this instance.

Ejemplos

El ejemplo siguiente busca el índice de la aparición de cualquier carácter de la cadena "Ayuda" en una subcadena de otra cadena.The following example finds the index of the occurrence of any character of the string "aid" within a substring of another string.

// Sample for String::IndexOfAny(Char[], Int32, Int32)
using namespace System;
int 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;
   int count;
   String^ target = "aid";
   array<Char>^anyOf = 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 )
      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)
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;
    int count;
    string target = "aid";
    char[] anyOf = 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) 
        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)
Imports System

Class Sample
   Public Shared Sub Main()
      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 'Main
End Class 'Sample
'
'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 startIndex + count -1.The search begins at startIndex and continues to startIndex + count -1. El carácter que ocupa startIndex + count no se incluye en la búsqueda.The character at startIndex + count is not included in the search.

Índice de la numeración empieza desde cero.Index numbering starts from zero. El startIndex parámetro puede oscilar entre 0 y uno menos que la longitud de la instancia de cadena.The startIndex parameter can range from 0 to one less than the length of the string instance.

La búsqueda de anyOf distingue mayúsculas de minúsculas.The search for anyOf is case-sensitive.

Este método realiza una búsqueda ordinal (sin tener en cuenta la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores escalares Unicode son iguales.This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar value are the same. Para realizar una búsqueda de referencias culturales, utilice el CompareInfo.IndexOf método, donde un valor escalar Unicode que representa un carácter compuesto, como la ligadura "Æ" U + 00 C 6, es posible que se considera equivalente a cualquier aparición de los componentes del carácter en el Corrija la secuencia, como "AE" (u+0041, u+0045), en función de la referencia cultural.To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

Consulte también:

Se aplica a