StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} {1}", "Jeffrey", "Richer").Replace(" ","-");
String s = sb.ToString().ToUpper();
sb.Length = 0;
sb.Append(s).Insert(8, "Marc-");
s = sb.ToString();
Console.WriteLine(s);
如果要动态的创建字符串,应该用StringBuilder,因为String类是一个不变的字符串。StringBuilder是可变的,修改时修改的是实际字符串,而不是副本。
String s="aa";
for(int i=0;i<5;i++)
{
s+=i.ToString()+" ";
}这样连接字符串 ,这样效率不够高,
个人建议用StringBuilder创建
StringBuilder s1 = new StringBuilder();
s1.AppendFormat("{0}{1}",s," ");
s.AppendFormat("{0}{1}",i," ");
for(int i=0;i<5;i++)
{
s1.AppendFormat("{0}{1}",i," ");
}