c# 以多个空格分割字符串
可以使用Split函数来以多个空格分割字符串,示例代码如下:
string str = "hello world how are you"; string[] words = str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words) { Console.WriteLine(word); }
输出结果为:
hello world how are you
在Split函数中,我们传入了包含一个空格字符的数组,该数组指定了要使用哪些字符分割字符串。由于我们使用了RemoveEmptyEntries选项,所以将会忽略连续的空格字符,只保留实际的单词。
原文地址: https://www.cveoy.top/t/topic/f4Gr 著作权归作者所有。请勿转载和采集!