logo

Metodo String.Split() in C# con esempi

In C#, Split() è un metodo di classe stringa. Il metodo Split() restituisce un array di stringhe generate dalla suddivisione della stringa originale separata dai delimitatori passati come parametro nel metodo Split(). I delimitatori possono essere un carattere, un array di caratteri o un array di stringhe. Oppure puoi anche dire che restituisce un array di stringhe che contiene le sottostringhe nell'istanza corrente delimitate da elementi di una stringa specificata o di un array di caratteri Unicode.
Ci sono 6 metodi nell'elenco di sovraccarico di questo metodo come segue:

Metodo Descrizione
Dividi(String[], Int32, StringSplitOptions) Suddivide la stringa in un numero massimo di sottostringhe in base all'array di stringhe passato come parametro. È possibile specificare se includere o meno gli elementi dell'array vuoto nell'array di sottostringhe.
Dividi(Char[], Int32, StringSplitOptions) Suddivide la stringa in un numero massimo di sottostringhe in base all'array di caratteri passato come parametro. È possibile specificare se includere o meno gli elementi dell'array vuoto nell'array di sottostringhe.
Dividi(String[], StringSplitOptions) Divide una stringa in sottostringhe in base alla matrice di stringhe. È possibile specificare se includere o meno gli elementi dell'array vuoto nell'array di sottostringhe.
Dividi(Car[]) Divide una stringa in sottostringhe in base alla matrice di caratteri.
Dividi(Car[], StringSplitOptions) Divide una stringa in sottostringhe in base alla matrice di caratteri. È possibile specificare se includere o meno gli elementi dell'array vuoto nell'array di sottostringhe.
Dividi(Char[], Int32) Suddivide la stringa in un numero massimo di sottostringhe in base all'array di caratteri passato come parametro. È possibile specificare il numero massimo di sottostringhe da restituire.

1. Metodo Split(String[], Int32, StringSplitOptions).

Questo metodo viene utilizzato per dividere una stringa in un numero massimo di sottostringhe in base alle stringhe in un array. È possibile specificare se le sottostringhe includono elementi di array vuoti.



Sintassi:

 public String[] Split(String[] separator, int count, StringSplitOptions options);>

parametri:

    separatore: è un array di stringhe che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null. count: è il numero massimo di sottostringhe da restituire. opzioni: Rimuovi voci vuote opzione per omettere elementi di array vuoti dall'array restituito o opzione Nessuna per includere elementi di array vuoti nell'array restituito.

Ritorno: Questo metodo restituisce un array i cui elementi contengono le sottostringhe in questa stringa delimitate da uno o più caratteri nel separatore.



Eccezioni:

    ArgumentOutOfRangeException: se il contare è negativo. ArgumentException: Se il opzioni non è uno dei valori StringSplitsOptions.

Esempio:






// C# program to illustrate the> // Split(String[], Int32, StringSplitOptions)> // Method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >String[] spearator = {>'s, '>,>'For'> };> >Int32 count = 2;> > >// using the method> >String[] strlist = str.Split(spearator, count,> >StringSplitOptions.RemoveEmptyEntries);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> >}> }>

>

>

Produzione:

 Geek Geeks>

2. Metodo Split(Char[], Int32, StringSplitOptions).

Questo metodo viene utilizzato per dividere una stringa in un numero massimo di sottostringhe in base ai caratteri in un array.

Sintassi:

 public String[] Split(char[] separator, int count, StringSplitOptions options);>

parametri:

    separatore: è un array di caratteri che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null. count: è il numero massimo di sottostringhe da restituire. opzioni: opzione RemoveEmptyEntries per omettere elementi di array vuoti dall'array restituito o opzione None per includere elementi di array vuoti nell'array restituito.

Ritorno: È un array i cui elementi contengono le sottostringhe di questa stringa delimitate da uno o più caratteri nel separatore.

Eccezioni:

    ArgumentOutOfRangeException: se il conteggio è negativo. ArgumentException: se le opzioni non sono uno dei valori StringSplitOptions.

Esempio:

pulsante nel css centrale




// C# program to illustrate the> // Split(Char[], Int32,> // StringSplitOptions) Method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >char>[] spearator = {>','>,>' '> };> >Int32 count = 2;> > >// Using the Method> >String[] strlist = str.Split(spearator,> >count, StringSplitOptions.None);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> >}> }>

>

>

Produzione:

 Geeks For Geeks>

3. Metodo Split(String[], StringSplitOptions).

Questo metodo viene utilizzato per dividere una stringa in sottostringhe in base alle stringhe in un array. È possibile specificare se le sottostringhe includono elementi di array vuoti.

Sintassi:

 public String[] Split(String[] separator, StringSplitOptions options);>

parametri:

    separatore: è un array di stringhe che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null. opzioni: opzione RemoveEmptyEntries per omettere elementi di array vuoti dall'array restituito o opzione None per includere elementi di array vuoti nell'array restituito.

Ritorna: Questo metodo restituisce una matrice di stringhe i cui elementi contengono le sottostringhe in questa stringa delimitate da uno o più caratteri nel separatore.

Eccezione: Questo metodo darà ArgumentException se la opzioni Il parametro non è uno dei valori StringSplitOptions.

Esempio:

impostazioni del browser web




// C# program to illustrate the> // Split(String[], StringSplitOptions)> // Method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >String[] spearator = {>'s,'>,>'For'> };> > >// using the method> >String[] strlist = str.Split(spearator,> >StringSplitOptions.RemoveEmptyEntries);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> >}> }>

>

>

Produzione:

 Geek Geeks>

4. Metodo Dividi(char[]).

Questo metodo viene utilizzato per dividere una stringa in sottostringhe basate sui caratteri in un array.

Sintassi:

 public String[] Split(char[] separator);>

Qui, separatore è un array di caratteri che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null.

Ritorna: Restituisce un array di stringhe i cui elementi contengono le sottostringhe in questa stringa delimitate da uno o più caratteri nel separatore.

Esempio:




// C# program to illustrate the> // Split(char[]) Method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >char>[] spearator = {>','>,>' '> };> > >// using the method> >String[] strlist = str.Split(spearator);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> >Console.ReadKey();> >}> }>

>

>

Produzione:

 Geeks For Geeks>

5. Metodo Split(char[], StringSplitOptions).

Questo metodo viene utilizzato per dividere una stringa in sottostringhe in base ai caratteri in un array. È possibile specificare se le sottostringhe includono elementi di array vuoti.

Sintassi:

 public String[] Split(char[] separator, StringSplitOptions option);>

parametri:

    separatore: è un array di caratteri che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null. opzioni: opzione RemoveEmptyEntries per omettere elementi di array vuoti dall'array restituito o opzione None per includere elementi di array vuoti nell'array restituito.

Ritorna: Questo metodo restituisce un array i cui elementi contengono le sottostringhe in questa stringa delimitate da uno o più caratteri nel separatore .

Esempio:




// C# program to illustrate the use of> // Split(Char[], StringSplitOptions) method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >char>[] spearator = {>','>,>' '> };> > >// using the method> >String[] strlist = str.Split(spearator,> >StringSplitOptions.RemoveEmptyEntries);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> >}> }>

tastiera pagina giù
>

>

Produzione:

 Geeks For Geeks>

6. Metodo Split(char[], Int32).

Questo metodo viene utilizzato per dividere una stringa in un numero massimo di sottostringhe in base ai caratteri in un array. Specifica inoltre il numero massimo di sottostringhe da restituire.

Sintassi:

 public String[] Split(char[] separator, Int32 count);>

parametri:

    separatore: un array di caratteri che delimita le sottostringhe in questa stringa, un array vuoto che non contiene delimitatori o null. count: è il numero massimo di sottostringhe da restituire.

Ritorna: Questo metodo restituisce un array i cui elementi contengono le sottostringhe in questa istanza delimitate da uno o più caratteri nel file separatore .

Eccezione: Questo metodo darà ArgumentOutOfRangeException se il conteggio è negativo.

Esempio:




// C# program to illustrate the use of> // Split(char[], Int32) Method> using> System;> > class> GFG {> > >// Main Method> >static> void> Main(>string>[] args)> >{> > >// Taking a string> >String str =>'Geeks, For Geeks'>;> > >char>[] spearator = {>','>,>' '> };> >Int32 count = 2;> > >// using the method> >String[] strlist = str.Split(spearator, count);> > >foreach>(String s>in> strlist)> >{> >Console.WriteLine(s);> >}> > >}> }>

>

>

Produzione:

 Geeks For Geeks>

Riferimento: