Informations
Le sujet que vous avez demandé est indiqué ci-dessous. Toutefois, ce sujet ne figure pas dans la bibliothèque.
Ce sujet n'a pas encore été évalué - Évaluez ce sujet

String.Split, méthode (Char[], StringSplitOptions)

Mise à jour : novembre 2007

Retourne un tableau de chaînes qui contient les sous-chaînes de cette chaîne, délimitées par les éléments d'un tableau de caractères Unicode spécifié. Un paramètre spécifie s'il faut retourner les éléments de tableau vides.

Espace de noms :  System
Assembly :  mscorlib (dans mscorlib.dll)
[ComVisibleAttribute(false)]
public string[] Split(
	char[] separator,
	StringSplitOptions options
)
/** @attribute ComVisibleAttribute(false) */
public String[] Split(
	char[] separator,
	StringSplitOptions options
)
public function Split(
	separator : char[], 
	options : StringSplitOptions
) : String[]

Paramètres

separator
Type : System.Char[]
Tableau de caractères Unicode qui délimitent les sous-chaînes de cette chaîne, tableau vide qui ne contient pas de délimiteur, ou null.
options
Type : System.StringSplitOptions
Spécifiez RemoveEmptyEntries pour ignorer les éléments de tableau vides du tableau retourné ou None pour inclure les éléments de tableau vides du tableau retourné.

Valeur de retour

Type : System.String[]
Tableau dont les éléments contiennent les sous-chaînes de cette chaîne qui sont délimitées par un ou plusieurs caractères dans separator. Pour plus d'informations, consultez la section Remarques.
ExceptionCondition
ArgumentException

options ne fait pas partie des valeurs StringSplitOptions.

Les caractères délimiteurs ne sont pas inclus dans les éléments du tableau retourné.

Si cette instance ne contient aucun des caractères de separator, le tableau retourné se compose d'un seul élément contenant cette instance. Si le paramètre separator est null ou ne contient pas de caractères, les délimiteurs sont supposés être des espaces blancs. Pour obtenir une liste des caractères que la méthode Split interprète comme espace blanc, consultez le tableau de la section Notes de la méthode String.Split(Char[]). (Notez que cette liste est légèrement différente de celle des espaces blancs reconnus par la méthode Trim().)

Chaque élément de separator définit un caractère délimiteur séparé. Si le paramètre options est RemoveEmptyEntries et que la longueur de cette instance est égale à zéro, un tableau vide est retourné.

Si le paramètre options est None et que deux délimiteurs sont adjacents ou qu'un délimiteur est trouvé au début ou à la fin de cette instance, l'élément de tableau correspondant contient Empty.

Considérations sur les performances

Les méthodes Split allouent de la mémoire pour l'objet tableau retourné et un objet String pour chaque élément du tableau. Si votre application requiert des performances optimales ou si la gestion de l'allocation de mémoire est critique dans votre application, envisagez d'utiliser la méthode IndexOf ou IndexOfAny, et éventuellement la méthode Compare, pour localiser une sous-chaîne dans une chaîne.

Si vous fractionnez une chaîne au caractère de séparation, utilisez la méthode IndexOf ou IndexOfAny pour rechercher un caractère de séparation dans la chaîne. Si vous fractionnez une chaîne au niveau d'une chaîne de séparation, utilisez la méthode IndexOf ou IndexOfAny pour rechercher le premier caractère de la chaîne de séparation. Puis utilisez la méthode Compare pour déterminer si les caractères suivant ce premier caractère sont égaux aux caractères restants de la chaîne de séparation.

De plus, si le même jeu de caractères est utilisé pour fractionner des chaînes en plusieurs appels de méthode Split, envisagez la création d'un tableau unique et son référencement dans chaque appel de méthode. Cela réduit considérablement la surcharge supplémentaire de chaque appel de méthode.

L'exemple de code suivant utilise l'énumération StringSplitOptions pour inclure ou exclure des sous-chaînes générées par la méthode Split.

// This example demonstrates the String() methods that use
// the StringSplitOptions enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    string s1 = ",ONE,,TWO,,,THREE,,";
    string s2 = "[stop]" +
                "ONE[stop][stop]" +
                "TWO[stop][stop][stop]" +
                "THREE[stop][stop]";
    char[] charSeparators = new char[] {','};
    string[] stringSeparators = new string[] {"[stop]"};
    string[] result;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
    Console.WriteLine("1) Split a string delimited by characters:\n");

// Display the original string and delimiter characters.
    Console.WriteLine("1a )The original string is \"{0}\".", s1);
    Console.WriteLine("The delimiter character is '{0}'.\n", 
                       charSeparators[0]);

// Split a string delimited by characters and return all elements.
    Console.WriteLine("1b) Split a string delimited by characters and " +
                      "return all elements:");
    result = s1.Split(charSeparators, StringSplitOptions.None);
    Show(result);

// Split a string delimited by characters and return all non-empty elements.
    Console.WriteLine("1c) Split a string delimited by characters and " +
                      "return all non-empty elements:");
    result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// Split the original string into the string and empty string before the 
// delimiter and the remainder of the original string after the delimiter.
    Console.WriteLine("1d) Split a string delimited by characters and " +
                      "return 2 elements:");
    result = s1.Split(charSeparators, 2, StringSplitOptions.None);
    Show(result);

// Split the original string into the string after the delimiter and the 
// remainder of the original string after the delimiter.
    Console.WriteLine("1e) Split a string delimited by characters and " +
                      "return 2 non-empty elements:");
    result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
    Console.WriteLine("2) Split a string delimited by another string:\n");

// Display the original string and delimiter string.
    Console.WriteLine("2a) The original string is \"{0}\".", s2);
    Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);

// Split a string delimited by another string and return all elements.
    Console.WriteLine("2b) Split a string delimited by another string and " +
                      "return all elements:");
    result = s2.Split(stringSeparators, StringSplitOptions.None);
    Show(result);

// Split the original string at the delimiter and return all non-empty elements.
    Console.WriteLine("2c) Split a string delimited by another string and " +
                      "return all non-empty elements:");
    result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// Split the original string into the empty string before the 
// delimiter and the remainder of the original string after the delimiter.
    Console.WriteLine("2d) Split a string delimited by another string and " +
                      "return 2 elements:");
    result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
    Show(result);

// Split the original string into the string after the delimiter and the 
// remainder of the original string after the delimiter.
    Console.WriteLine("2e) Split a string delimited by another string and " + 
                      "return 2 non-empty elements:");
    result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
    Show(result);
    }

// Display the array of separated strings.
    public static void Show(string[] entries)
    {
    Console.WriteLine("The return value contains these {0} elements:", entries.Length);
    foreach (string entry in entries)
        {
        Console.Write("<{0}>", entry);
        }
    Console.Write("\n\n");
    }
}
/*
This example produces the following results:

1) Split a string delimited by characters:

1a )The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.

1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>

1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>

1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>

2) Split a string delimited by another string:

2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".

2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>

2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>

2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>

*/


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professionnel Édition x64, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

© 2013 Microsoft. Tous droits réservés.