如何在 C# 中將一個字串拆分成一個字串陣列中的元素?
設定要拆分的字串。
string str = "Hello World!";
使用 split() 方法將字串拆分成單獨的元素。
string[] res = str.Split(' ');
下面是 C# 中將一個字串拆分成一個字串陣列中的元素的完整程式碼。
例子
using System; class Demo { static void Main() { string str = "Hello World!"; string[] res = str.Split(' '); Console.WriteLine("Separate elements:"); foreach (string words in res) { Console.WriteLine(words); } } }
輸出
Separate elements: Hello World!
廣告