首页 > 其他 > 详细

String.Split分隔字符串

时间:2020-04-01 00:08:19      阅读:117      评论:0      收藏:0      [点我收藏+]

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

 

String.Split分隔字符串

原文:https://www.cnblogs.com/bibi-feiniaoyuan/p/split.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!