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.Split (Char[], Int32)

 

Divide una cadena en un número máximo de subcadenas en función de los caracteres de una matriz. Número máximo de subcadenas que se va devolver.

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

public string[] Split(
	char[] separator,
	int count
)

Parámetros

separator
Type: System.Char[]

Matriz de caracteres que delimitan las subcadenas de esta cadena, una matriz vacía sin delimitadores o null.

count
Type: System.Int32

Número máximo de subcadenas que se van devolver.

Valor devuelto

Type: System.String[]

Una matriz cuyos elementos contienen las subcadenas de esta instancia que están delimitadas por uno o varios caracteres de separator. Para obtener más información, vea la sección Comentarios.

Exception Condition
ArgumentOutOfRangeException

count es negativo.

Delimiter characters are not included in the elements of the returned array.

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance. If count is zero, an empty array is returned.

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the M:System.Char.IsWhiteSpace(System.Char) method.

Each element of separator defines a separate delimiter character. If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains F:System.String.Empty.

If there are more than count substrings in this instance, the first count minus 1 substrings are returned in the first count minus 1 elements of the return value, and the remaining characters in this instance are returned in the last element of the return value.

If count is greater than the number of substrings, the available substrings are returned and no exception is thrown.

The following table provides examples.

String value

Separator

Count

Returned array

"42, 12, 19"

new Char[] {',', ' '} (C#)

Char() = {","c, " "c} (Visual Basic)

2

{"42", " 12, 19"}

"42..12..19"

new Char[] {'.'} (C#)

Char() = {"."c} (Visual Basic)

4

{"42", "", "12", ".19"}

"Banana"

new Char[] {'.'} (C#)

Char() = {"."c} (Visual Basic)

2

{"Banana"}

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] {} (C#)

Char() = {} (Visual Basic)

1

{"Darb\nSmarba"} (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] null (C#)

Char() = Nothing

2

{"Darb", "Smarba"}

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] null (C#)

Char() = Nothing

100

{"Darb", "Smarba"}

TheOverload:System.String.Split methods allocate memory for the returned array object and a T:System.String object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method, and optionally the Overload:System.String.Compare method, to locate a substring within a string.

If you are splitting a string at a separator character, use the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method to locate the first character of the separator string. Then use the Overload:System.String.Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.

In addition, if the same set of characters is used to split strings in multiple Overload:System.String.Split method calls, consider creating a single array and referencing it in each method call. This significantly reduces the additional overhead of each method call.

Notas para llamadores:

In the net_v35_short and earlier versions, if the M:System.String.Split(System.Char[]) method is passed a separator that is null or contains no characters, the method uses a slightly different set of characters to split the string than the M:System.String.Trim(System.Char[]) method does to trim the string. In the net_v40_short, both methods use an identical set of Unicode white-space characters.

The following example demonstrates how count affects the number of strings returned by M:System.String.Split(System.Char[]).

using System;

public class Example
{
   public static void Main() 
   {
      string delimStr = " ,.:";
      char [] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string [] split = null;

      Console.WriteLine("The delimiters are -{0}-", delimStr);
      for (int x = 1; x <= 5; x++) {
         split = words.Split(delimiter, x);
         Console.WriteLine("\ncount = {0,2} ..............", x);
         foreach (var s in split) {
             Console.WriteLine("-{0}-", s);
         }
      }
   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --

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