String.Split分隔字符串
程序员文章站
2022-04-27 18:29:58
一种char分隔符 string phrase = "The quick brown fox jumps over the lazy dog."; string[] words = phrase.Split(' '); foreach (var word in words) { System.Con ......
一种char分隔符
string phrase = "the quick brown fox jumps over the lazy dog."; string[] words = phrase.split(' '); foreach (var word in words) { system.console.writeline($"<{word}>"); }
分隔之后的结果,去掉多余的空格
// stringsplitoptions.removeemptyentries移除多余的空格 string phrase = "the quick brown fox jumps over the lazy dog."; string[] words = phrase.split(new char[] { ' ' }, stringsplitoptions.removeemptyentries); foreach (var word in words) { system.console.writeline($"<{word}>"); }
多种char分隔符
// 使用多个分隔符 char[] delimiterchars = { ' ', ',', '.', ':', '\t' }; string text = "one\ttwo three:four,five six seven"; system.console.writeline($"original text: '{text}'"); // public string[] split(params char[] separator); string[] words = text.split(delimiterchars); system.console.writeline($"{words.length} words in text:"); foreach (var word in words) { system.console.writeline($"<{word}>"); }
多种string分隔符
string[] separatingstrings = { "<<", "..." }; string text = "one<<two......three<four"; system.console.writeline($"original text: '{text}'"); //public string[] split(string[] separator, stringsplitoptions options); string[] words = text.split(separatingstrings, system.stringsplitoptions.removeemptyentries); system.console.writeline($"{words.length} substrings in text:"); foreach (var word in words) { system.console.writeline(word); }
上一篇: 55岁的人负债一百多万,想回农村搞养殖,养什么可以翻身呢?
下一篇: 凤眼果应该如何辨别真假?