当前位置: 移动技术网 > IT编程>开发语言>c# > String.Split分隔字符串

String.Split分隔字符串

2020年04月01日  | 移动技术网IT编程  | 我要评论

一种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);
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网