如果只是字符串进行拼加建议用StringBuilder str = new StringBuilder(); str.Append();
string s = ""; for (int i = 0; i < 100; i++) { s += i.ToString(); } Console.Write(s); s = ""; for (int i = 0; i < 100; i++) { s = String.Concat(s, i.ToString()); } Console.Write(s); //reflector int i; string s = ""; for (i = 0; i < 100; i++) { s = s + i.ToString(); } Console.Write(s); s = ""; for (i = 0; i < 100; i++) { s = s + i.ToString(); } Console.Write(s);
//建议楼主多去查阅MSDN //reflector int i; string s = ""; for (i = 0; i < 100; i++) { s = s + i.ToString();//此处的ToString()是多余的 } Console.Write(s); s = ""; for (i = 0; i < 100; i++) { s = s + i.ToString();//同上 } Console.Write(s);
Console.Write(String.Concat("a", "b", "c"));//如果参数不是字符串!会先将其转换成字符串再执行操作//你那样看其实看不到什么
str.Append();
string s = "";
for (int i = 0; i < 100; i++)
{
s += i.ToString();
}
Console.Write(s); s = "";
for (int i = 0; i < 100; i++)
{
s = String.Concat(s, i.ToString());
}
Console.Write(s);
//reflector
int i;
string s = "";
for (i = 0; i < 100; i++)
{
s = s + i.ToString();
}
Console.Write(s);
s = "";
for (i = 0; i < 100; i++)
{
s = s + i.ToString();
}
Console.Write(s);
//建议楼主多去查阅MSDN
//reflector
int i;
string s = "";
for (i = 0; i < 100; i++)
{
s = s + i.ToString();//此处的ToString()是多余的
}
Console.Write(s);
s = "";
for (i = 0; i < 100; i++)
{
s = s + i.ToString();//同上
}
Console.Write(s);
{
return (str0 + str1);
}
stringbulider比较好吧。。
String.Concat()
+ 操作
String.Format()
字符串连接性能
若有大量的数据需要连接,就用StingBulider来控制,就Ok了
public static string Concat(string str0, string str1)
{
if (IsNullOrEmpty(str0))
{
if (IsNullOrEmpty(str1))
{
return Empty;
}
return str1;
}
if (IsNullOrEmpty(str1))
{
return str0;
}
int length = str0.Length;
string dest = FastAllocateString(length + str1.Length);
FillStringChecked(dest, 0, str0);
FillStringChecked(dest, length, str1);
return dest;
}
private static unsafe void FillStringChecked(string dest, int destPos, string src)
{
int length = src.Length;
if (length > (dest.Length - destPos))
{
throw new IndexOutOfRangeException();
}
fixed (char* chRef = &dest.m_firstChar)
{
fixed (char* chRef2 = &src.m_firstChar)
{
wstrcpy(chRef + destPos, chRef2, length);
}
}
} private static unsafe void wstrcpy(char* dmem, char* smem, int charCount)
{
if (charCount > 0)
{
if ((((int) dmem) & 2) != 0)
{
dmem[0] = smem[0];
dmem++;
smem++;
charCount--;
}
while (charCount >= 8)
{
*((int*) dmem) = *((uint*) smem);
*((int*) (dmem + 2)) = *((uint*) (smem + 2));
*((int*) (dmem + 4)) = *((uint*) (smem + 4));
*((int*) (dmem + 6)) = *((uint*) (smem + 6));
dmem += 8;
smem += 8;
charCount -= 8;
}
if ((charCount & 4) != 0)
{
*((int*) dmem) = *((uint*) smem);
*((int*) (dmem + 2)) = *((uint*) (smem + 2));
dmem += 4;
smem += 4;
}
if ((charCount & 2) != 0)
{
*((int*) dmem) = *((uint*) smem);
dmem += 2;
smem += 2;
}
if ((charCount & 1) != 0)
{
dmem[0] = smem[0];
}
}
}
public StringBuilder Append(string value)
{
if (value != null)
{
string stringValue = this.m_StringValue;
IntPtr currentThread = Thread.InternalGetCurrentThread();
if (this.m_currentThread != currentThread)
{
stringValue = string.GetStringForStringBuilder(stringValue, stringValue.Capacity);
}
int length = stringValue.Length;
int requiredLength = length + value.Length;
if (this.NeedsAllocation(stringValue, requiredLength))
{
string newString = this.GetNewString(stringValue, requiredLength);
newString.AppendInPlace(value, length);
this.ReplaceString(currentThread, newString);
}
else
{
stringValue.AppendInPlace(value, length);
this.ReplaceString(currentThread, stringValue);
}
}
return this;
}
"a", "b", "c" 是常量 编译器自动优化成 "abc"改成 string a = "a", b = "b", c = "c";
Console.WriteLine(a + b + c);
Console.WriteLine(string.Concat(a,b,c));a + b + c 与 string.Concat(a,b,c) 生成的IL一模一样
string a = "a" + "b" + "c";
0000003d mov eax,dword ptr ds:[022D21CCh] //从这句可以看到结果是已经存在 ,也就是说编译器做个优化了,内存里直接放了a+b+c的结果
00000043 mov dword ptr [ebp-44h],eax
string b = String.Concat("a", "b", "c");
00000046 push dword ptr ds:[022D21D8h]
0000004c mov ecx,dword ptr ds:[022D21D0h]
00000052 mov edx,dword ptr ds:[022D21D4h] //从上3句可以看到,3个参数分别放在3个地址,此时结果不存在
00000058 call 7118A300 //调用contcat方法,计算结果
0000005d mov dword ptr [ebp-4Ch],eax
00000060 mov eax,dword ptr [ebp-4Ch]
00000063 mov dword ptr [ebp-48h],eax
楼主的问题你可以看看自己的IL代码,Reflector里面选项里面,选择IL即可。
Concat类似于C/C++里的对Char[]数据的原始操作,效率肯定比不上直接来一个新的字符串高
+是速度最快的,但短时间内占用内存是最大的,它就象C/C++里的strCopy方法
Concat或者StringBuilder是最节省内存的,但是处理速度是最慢的,它就象是直接操作一个char*以上观点来源于我以前学习C/C++时的认识,不同意见者,欢迎交流