C# Split 分割字符串
作者:野牛程序员:2023-12-25 14:46:36C#阅读 2556
使用C#中的Split
方法可以很容易地分割字符串。以下是一个简单的例子:
string input = "Hello,World,How,Are,You"; char[] separators = { ',' }; string[] result = input.Split(separators); foreach (string s in result) { Console.WriteLine(s); }
这个例子中,Split
方法使用逗号作为分隔符,将输入字符串分割成一个字符串数组。然后,使用foreach
循环遍历数组并打印每个分割后的字符串。
using System; class Program { static void Main() { string input = "Hello,World,How,Are,You"; char[] separators = { ',' }; string[] result = input.Split(separators); foreach (string s in result) { Console.WriteLine(s); } } }
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
