String.Format का उपयोग करता StringBuilder
आंतरिक रूप से ... इसलिए तार्किक है कि इस विचार की ओर जाता है कि यह अधिक भूमि के ऊपर की वजह से थोड़ा कम performant होगा। हालांकि, एक साधारण स्ट्रिंग कॉन्सटेनेशन एक महत्वपूर्ण डिग्री से दो अन्य लोगों के बीच एक स्ट्रिंग इंजेक्शन करने का सबसे तेज़ तरीका है। यह साक्ष्य रिको मारियानी ने कई साल पहले अपने पहले प्रदर्शन क्विज़ में प्रदर्शित किया था। सरल तथ्य यह है कि concatenations ... जब स्ट्रिंग भागों की संख्या ज्ञात है (बिना सीमा के..तुम एक हजार भागों को जोड़ सकते हैं ... जब तक आप इसे हमेशा 1000 भागों जानते हैं) ... StringBuilder
या स्ट्रिंग से हमेशा तेज .Format। उन्हें एक स्मृति स्मृति आवंटन के साथ एक स्मृति स्मृति आवंटन के साथ किया जा सकता है। Here यहाँ सबूत
है और (परावर्तक के माध्यम से निकाली गई) कुछ String.Concat तरीकों, जो अंततः FillStringChecked फोन जो स्मृति को कॉपी करने के संकेत दिए गए का उपयोग करता है के लिए वास्तविक कोड है:
public static string Concat(params string[] values)
{
int totalLength = 0;
if (values == null)
{
throw new ArgumentNullException("values");
}
string[] strArray = new string[values.Length];
for (int i = 0; i < values.Length; i++)
{
string str = values[i];
strArray[i] = (str == null) ? Empty : str;
totalLength += strArray[i].Length;
if (totalLength < 0)
{
throw new OutOfMemoryException();
}
}
return ConcatArray(strArray, totalLength);
}
public static string Concat(string str0, string str1, string str2, string str3)
{
if (((str0 == null) && (str1 == null)) && ((str2 == null) && (str3 == null)))
{
return Empty;
}
if (str0 == null)
{
str0 = Empty;
}
if (str1 == null)
{
str1 = Empty;
}
if (str2 == null)
{
str2 = Empty;
}
if (str3 == null)
{
str3 = Empty;
}
int length = ((str0.Length + str1.Length) + str2.Length) + str3.Length;
string dest = FastAllocateString(length);
FillStringChecked(dest, 0, str0);
FillStringChecked(dest, str0.Length, str1);
FillStringChecked(dest, str0.Length + str1.Length, str2);
FillStringChecked(dest, (str0.Length + str1.Length) + str2.Length, str3);
return dest;
}
private static string ConcatArray(string[] values, int totalLength)
{
string dest = FastAllocateString(totalLength);
int destPos = 0;
for (int i = 0; i < values.Length; i++)
{
FillStringChecked(dest, destPos, values[i]);
destPos += values[i].Length;
}
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);
}
}
}
तो फिर:
string what = "cat";
string inthehat = "The " + what + " in the hat!";
आनंद लें!
भविष्य में सुधार की अनुमति देने के लिए कृपया इन्हें खोलें। –
एक विशेष मामले परिदृश्य में, इनमें से सबसे तेज़ नहीं है: यदि भाग को प्रतिस्थापित किया जाए तो आकार के बराबर बराबर है, तो आप स्ट्रिंग को जगह में बदल सकते हैं। दुर्भाग्य से, इसके लिए प्रतिबिंब या असुरक्षित कोड की आवश्यकता होती है और जानबूझकर स्ट्रिंग की अपरिवर्तनीयता का उल्लंघन करती है। एक अच्छा अभ्यास नहीं है, लेकिन यदि गति एक मुद्दा है ... :) – Abel
ऊपर दिए गए उदाहरण में '' 'स्ट्रिंग एस =" टोपी में "+ बिल्ली +" ";' '' '' 'का उपयोग तब तक किया जा सकता है जब तक इसका उपयोग नहीं किया जाता एक लूप में, जिस मामले में सबसे तेज़ लूप के बाहर ''' स्ट्रिंगबिल्डर' '' के साथ शुरू होगा। –