通过+、+=连接。
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);