C# | Índice de la primera aparición en StringCollection

La clase StringCollection es una nueva adición a la biblioteca de clases de .NET Framework que representa una colección de strings. La clase StringCollection se define en el espacio de nombres System.Collections.Specialized .
El método StringCollection.IndexOf(String) se utiliza para buscar la string especificada que devuelve el índice basado en cero de la primera aparición dentro de StringCollection.

Sintaxis:

public int IndexOf (string value);

Aquí, el valor es la string a localizar. El valor puede ser nulo.

Valor devuelto: el método devuelve un índice basado en cero de la primera aparición de valor en StringCollection; si se encuentra de otra manera, devuelve -1.

Nota: este método realiza una búsqueda lineal . Por lo tanto, este método es una operación O(n), donde n es Count.

Los siguientes programas ilustran el uso del método StringCollection.IndexOf(String) :

Ejemplo 1:

// C# code to search string and returns
// the zero-based index of the first
// occurrence in StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "A", "B", "C", "C", "D" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        // To search string "C" and return
        // the zero-based index of the first
        // occurrence in StringCollection
        // Here, "C" exists at index 2 and 3.
        // But, the method should return 2
        Console.WriteLine(myCol.IndexOf("C"));
    }
}
Producción:

2

Ejemplo 2:

// C# code to search string and returns
// the zero-based index of the first
// occurrence in StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "2", "3", "4", "5", "6" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        // To search string "9" and return
        // the zero-based index of the first
        // occurrence in StringCollection
        // Here, "9" does not exist in myCol
        // Hence, method returns -1
        Console.WriteLine(myCol.IndexOf("9"));
    }
}
Producción:

-1

Referencia:

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *