📜  C#中的String.Split()方法与示例

📅  最后修改于: 2021-05-30 01:09:04             🧑  作者: Mango

在C#中,Split()是字符串类方法。 Split()方法返回一个字符串数组,该字符串数组是通过拆分原始字符串生成的,这些字符串由在Split()方法中作为参数传递的定界符分隔。定界符可以是一个或多个的阵列或字符串数组。或者,您也可以说它返回一个字符串数组,其中包含当前实例中的子字符串,这些子字符串由指定字符串或Unicode字符数组的元素定界。
此方法的重载列表中有6种方法,如下所示:

Method Description
Split(String[], Int32, StringSplitOptions) Split string into maximum number of sub strings based on the array of strings passed as parameter. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[], Int32, StringSplitOptions) Split string into maximum number of sub strings based on the array of characters passed as parameter. You can specify whether to include the empty array elements in array of sub strings or not.
Split(String[], StringSplitOptions) Splits a string into substrings based on the array of strings. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[]) Splits a string into substrings based on the array of characters.
Split(Char[], StringSplitOptions) Splits a string into substrings based on the array of characters. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[], Int32) Split string into maximum number of sub strings based on the array of characters passed as parameter. You can specify maximum number of sub strings to return.

1. Split(String [],Int32,StringSplitOptions)方法

此方法用于根据数组中的字符串将字符串拆分为最大数目的子字符串。您可以指定子字符串是否包含空数组元素。

句法:

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

参数:

  • 分隔符:这是一个字符串数组,用于分隔该字符串的子字符串,一个不包含任何分隔符的空数组,或者为null。
  • count:这是要返回的最大子字符串数。
  • 选项: RemoveEmptyEntries选项可从返回的数组中省略空数组元素,而None选项可在返回的数组中包括空数组元素。

返回值:此方法返回一个数组,该数组的元素包含此字符串中的子字符串,这些子字符串由分隔符中的一个或多个字符定界。

例外情况:

  • ArgumentOutOfRangeException:如果计数为负。
  • ArgumentException:如果选项不是StringSplitsOptions值之一。

例子:

// 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);
        }
    }
}

输出:

Geek
 Geeks

2. Split(Char [],Int32,StringSplitOptions)方法

此方法用于根据数组中的字符将字符串拆分为最大数量的子字符串。

句法:

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

参数:

  • 分隔符:这是一个字符数组,用于分隔此字符串的子字符串,一个不包含任何分隔符的空数组,或者为null。
  • count:这是要返回的最大子字符串数。
  • 选项: RemoveEmptyEntries选项可从返回的数组中省略空数组元素,而None选项可在返回的数组中包括空数组元素。

返回:一个数组,其元素包含此字符串中的子字符串,这些子字符串由分隔符中的一个或多个字符定界。

例外情况:

  • ArgumentOutOfRangeException:如果计数为负。
  • ArgumentException:如果选项不是StringSplitOptions值之一。

例子:

// 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);
        }
    }
}

输出:

Geeks
 For Geeks

3. Split(String [],StringSplitOptions)方法

此方法用于根据数组中的字符串将字符串拆分为子字符串。您可以指定子字符串是否包含空数组元素。

句法:

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

参数:

  • 分隔符:这是一个字符串数组,用于分隔该字符串中的子字符串;一个不包含任何分隔符的空数组;或者为null。
  • 选项: RemoveEmptyEntries选项可从返回的数组中省略空数组元素,而None选项可在返回的数组中包括空数组元素。

返回:此方法返回一个字符串数组,其元素包含此字符串中的子字符串,这些子字符串由分隔符中的一个或多个字符分隔。

异常:如果options参数不是StringSplitOptions值之一,则此方法将提供ArgumentException

例子:

// 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);
        }
    }
}

输出:

Geek
 
 Geeks

4. Split(char [])方法

此方法用于将字符串拆分为基于数组中字符的子字符串。

句法:

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

在这里,分隔符是一个字符数组,用于分隔该字符串中的子字符串,一个不包含任何分隔符的空数组或null。

返回:返回一个字符串数组,其元素包含此字符串中的子字符串,这些子字符串由分隔符中的一个或多个字符定界。

例子:

// 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();
    }
}

输出:

Geeks

For
Geeks

5. Split(char [],StringSplitOptions)方法

此方法用于根据数组中的字符将字符串拆分为子字符串。您可以指定子字符串是否包含空数组元素。

句法:

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

参数:

  • 分隔符:这是一个字符数组,用于分隔此字符串的子字符串,一个不包含任何分隔符的空数组,或者为null。
  • 选项: RemoveEmptyEntries选项可从返回的数组中省略空数组元素,而None选项可在返回的数组中包括空数组元素。

返回:此方法返回的数组,其元素包含在该字符串是通过在分离器的一个或多个字符分隔的子串。

例子:

// 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);
        }
    }
}

输出:

Geeks
For
Geeks

6. Split(char [],Int32)方法

此方法用于根据数组中的字符将字符串拆分为最大数量的子字符串。您还可以指定要返回的最大子字符串数。

句法:

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

参数:

  • 分离器:一个字符数组界定子在此字符串,不包含定界符或null空数组。
  • count:这是要返回的最大子字符串数。

返回:此方法返回一个数组,该数组的元素包含此实例中的子字符串,这些子字符串由分隔符中的一个或多个字符分隔

异常:如果计数为负,则此方法将提供ArgumentOutOfRangeException

例子:

// 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);
        }
          
    }
}

输出:

Geeks
 For Geeks

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system。字符串.split?view = netframework-4.8