如何在 C# 中将字符串拆分为字符串数组的元素?
csharpprogrammingserver side programming更新于 2025/7/10 11:52:17
设置要拆分的字符串。
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!

