Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método String.Contains (String)

 

Devuelve un valor que indica si una subcadena especificada aparece dentro de esta cadena.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

public bool Contains(
	string value
)

Parámetros

value
Type: System.String

Cadena que se va a buscar.

Valor devuelto

Type: System.Boolean

Es true si el parámetro value aparece en esta cadena o si value es la cadena vacía (""); en caso contrario, es false.

Exception Condition
ArgumentNullException

El valor de value es null.

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.

To determine whether a string contains a specified substring by using something other than ordinal comparison (such as culture-sensitive comparison, or ordinal case-insensitive comparison), you can create a custom method. The following example illustrates one such approach. It defines a T:System.String extension method that includes a T:System.StringComparison parameter and indicates whether a string contains a substring when using the specified form of string comparison.

using System;

public static class StringExtensions
{
   public static bool Contains(this String str, String substring, 
                               StringComparison comp)
   {                            
      if (substring == null)
         throw new ArgumentNullException("substring", 
                                         "substring cannot be null.");
      else if (! Enum.IsDefined(typeof(StringComparison), comp))
         throw new ArgumentException("comp is not a member of StringComparison",
                                     "comp");

      return str.IndexOf(substring, comp) >= 0;                      
   }
}

The following example then calls the Contains extension method to determine whether a substring is found in a string when using ordinal comparison and case-insensitive ordinal comparison.

using System;

public class Example
{
   public static void Main()
   {
      String s = "This is a string.";
      String sub1 = "this";
      Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
      StringComparison comp = StringComparison.Ordinal;
      Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));

      comp = StringComparison.OrdinalIgnoreCase;
      Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));
   }
}
// The example displays the following output:
//       Does 'This is a string.' contain 'this'?
//          Ordinal: False
//          OrdinalIgnoreCase: True

If you are interested in the position of the substring value in the current instance, you can call the M:System.String.IndexOf(System.String) method to get the starting position of its first occurrence, or you can call the M:System.String.LastIndexOf(System.String) method to get the starting position of its last occurrence. The example includes a call to the M:System.String.IndexOf(System.String) method if a substring is found in a string instance.

The following example determines whether the string "fox" is a substring of a familiar quotation. If "fox" is found in the string, it also displays its starting position.

using System;

class Example
{
    public static void Main() 
    {
       string s1 = "The quick brown fox jumps over the lazy dog";
       string s2 = "fox";
       bool b = s1.Contains(s2);
       Console.WriteLine("'{0}' is in the string '{1}': {2}",
                          s2, s1, b);
       if (b) {
          int index = s1.IndexOf(s2);
          if (index >= 0)
             Console.WriteLine("'{0} begins at character position {1}",
                               s2, index + 1);
       }
    }
}
// This example display the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17

Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 2.0
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio
Mostrar: