C# 字串分割()

王林
發布: 2024-09-03 15:17:42
原創
306 人瀏覽過

將給定的由分隔符號分隔的字串分割成字串陣列的方法稱為C# String Split()方法,分割的分隔符號是由字串組成的陣列或陣列由字元或僅字元組成,以及由子字串組成的字串數組,其分隔符號是Unicode 字元數組或指定字串的元素,透過使用C# 中的Split() 方法返回,並引發ArgumentOutofRangeException 和ArgumentException 例外使用此方法時的例外處理。

文法

C# String Split() 方法的語法如下:

public String[] Split(String[] separator, int count, StringSplitOptions options);
public String[] Split(params Char[] character)
public String[] Split(Char[], Int32)
public String[] Split(Char[], Int32, StringSplitOptions)
public String[] Split(Char[], StringSplitOptions)
public String[] Split(String[], Int32, StringSplitOptions)
public String[] Split(String[], StringSplitOptions)
登入後複製

其中分隔符號是分隔給定字串中子字串的字串陣列

計數記錄要傳回的子字串的最大數量

選項可以刪除空白條目選項以丟棄傳回數組中為空的數組元素,或選項 none 以包含傳回數組中的空數組元素。

C# String Split() 方法的工作原理

  • 每當需要根據分隔字串陣列或字元陣列或僅字元的分隔符號來分割字串時,我們都會使用 String split() 方法。
  • 分隔字串的分隔符號可以是字串數組或字元數組,或只是字元。
  • 使用字串 split() 方法傳回根據給定分隔符號分隔的給定字串的子字串。
  • 使用 Split() 方法時,ArgumentOutofRangeException 和 ArgumentException 例外值會作為例外處理的一部分引發。

C# String Split() 範例

下面給出了 C# String Split() 的範例:

範例#1

C# 程式示範 String Split() 方法,以逗號分隔字串

代碼:

using System;
//a class called check is defined
public class check
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string consisting of delimiters
string str = "Welcome,to,C,Sharp";
//a string array is used to store the array of strings returned by using string split method
string[] str2 = str.Split(',');
foreach (string s in str2)
{
Console.WriteLine(s);
}
}
}
登入後複製

輸出:

C# 字串分割()

在上面的程式中,呼叫了一個名為check的類別。然後呼叫main方法,其中定義一個字串變數來儲存由分隔符號組成的字串,分隔符號可用於將給定的字串分隔成子字串數組,然後定義一個字串數組來儲存返回的子字串數組透過使用顯示為輸出的字串split() 方法。輸出如上面的快照所示。

範例#2

C# 程式示範字串 split() 方法,將由多個分隔符號組成的給定字串分隔成字串陣列:

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is used to store the string consisting of multiple delimiters
string str1 = "Welcome,to-C|Sharp";
//a string array is defined to store the array of substrings returned by using the split() method on the given string
string[] str2 = str1.Split(new char[] { ',', '-', '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < str2.Length; j++)
{
Console.WriteLine(str2[j]);
}
Console.WriteLine("\nThis is how split() method works");
Console.ReadLine();
}
}
}
登入後複製

輸出:

C# 字串分割()

在上面的程式中,建立了一個名為program的命名空間。然後定義名為 check 的類,在該類別中呼叫 main 方法。然後定義一個字串變數來儲存由多個分隔符號組成的字串。然後我們使用 split() 方法將給定的字串分成顯示為輸出的子字串陣列。輸出如上面的快照所示。

範例#3

C# 程式示範 String IndexOf() 方法分隔由多個分隔符號組成的給定字串並將傳回值儲存在清單中:

代碼:

using System;
using System.Collections.Generic;
//a namespace called program is created
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is defined to store the string consisting of multiple delimiters
string str = "Welcome-to,C|Sharp";
//a list is defined to store the substrings separated from the given string consisting of delimiters
IList<string> listname = new List<string>(str.Split(new char[] { '-', ',', '|' }, StringSplitOptions.RemoveEmptyEntries));
for (int j = 0; j < listname.Count; j++)
{
Console.WriteLine(list[j]);
}
Console.WriteLine("\nThis is how split method works");
Console.ReadLine();
}
}
}
登入後複製

輸出:

C# 字串分割()

在上面的程式中,建立了一個名為program的命名空間。然後定義一個名為 check 的類,在該類別中呼叫 main 方法。然後定義一個字串變數來儲存由多個分隔符號組成的字串。然後,透過使用字串 split() 方法,可以透過建立新列表將給定字串拆分為儲存在列表中的字串數組。輸出如上面的快照所示。

以上是C# 字串分割()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!