bibi-feiniaoyuan

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

 

分类:

技术点:

相关文章:

  • 2021-05-22
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
猜你喜欢
  • 2021-11-01
  • 2022-02-15
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
相关资源
相似解决方案