VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 字符串连接

通过+、+=连接。

var str = "aa" + "bb" + "cc";
str += "dd";

字符串内插

var str = $"aa{(5+9)}bb{(89+90)}";

使用StringBuilder

复制代码
var sb = new System.Text.StringBuilder();
var sb1 = new System.Text.StringBuilder();
for (int i = 0; i < 20; i++)
{
    sb.AppendLine(i.ToString());
    sb1.Append(i.ToString());
}
System.Console.WriteLine(sb.ToString());
System.Console.WriteLine(sb1.ToString());
复制代码

 Concat、Join

复制代码
 string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };

 // Concat直接连接
 var unreadablePhrase = string.Concat(words);
 System.Console.WriteLine(unreadablePhrase);

 // Join加入分隔符
 var readablePhrase = string.Join(" ",words);
 System.Console.WriteLine(readablePhrase);
复制代码

Linq的Aggregate累加器函数

string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };

var phrase = words.Aggregate((partialPhrase, word) => $"{partialPhrase}+{word}-");
System.Console.WriteLine(phrase);

 

量变会引起质变。

相关教程