关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

String.Split分隔字符串的使用

发布时间:2020-04-02 00:00:00

一种char分隔符

string phrase = "The quick brown fox jumps over the lazy dog.";string[] words = phrase.Split(' ');foreach (var word in words)
{
    System.Console.WriteLine($"");
}

 

分隔之后的结果,去掉多余的空格

// 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($"");
}

 

多种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($"");
}

 

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

 


/template/Home/Zkeys/PC/Static