In C#, there are many ways to merge strings, and the performance and applicable scenarios are different in different ways. The following are common methods and their characteristics:
1. Use the + or += operator
string str1 = "Hello"; string str2 = "World"; string result = str1 + " " + str2;
Features
- Simple syntax and intuitive code.
- Suitable for small-scale string stitching operations.
performance
- Each splicing creates a new string object (the string is immutable).
- Low performance, especially when used frequently in loops, as it will lead to the creation of a large number of temporary objects and memory allocation.
Applicable scenarios
- A small number of string splicing (such as a small string in fixed format splicing).
2. Usage Method
string result = ("Hello", " ", "World");
Features
- No additional temporary objects are created.
- Directly concatenate multiple strings, performance ratio
+
Operator is high.
performance
- Compare
+
Fast because it avoids redundant operations generated by the compiler.
Applicable scenarios
- When multiple known strings need to be merged.
3. Usage Method
string result = ("{0} {1}", "Hello", "World");
Features
- Suitable for scenarios where strings need to be dynamically formatted.
- It is highly readable, especially when multiple variables need to be inserted.
performance
- The performance is slightly lower, as it involves parsing the formatted string.
Applicable scenarios
- Dynamically format the scene, string templates need to be clearly expressed.
4. Use StringBuilder
StringBuilder sb = new StringBuilder(); ("Hello"); (" "); ("World"); string result = ();
Features
- Efficient string splicing method, especially suitable for a large number of splicing operations.
- It can be expanded dynamically without knowing the length of the string in advance.
performance
- High performance because multiple creation of temporary string objects is avoided.
- Especially suitable for handling large numbers of strings in loops.
Applicable scenarios
- A lot of string splicing, especially when used in loops.
5. Use interpolated strings (C# 6.0 and later)
string name = "World"; string result = $"Hello {name}";
Features
- High readability and concise syntax.
- It will be converted to
。
Performance
- Performance and
resemblance.
- Better readability
, but not as good as high-performance demand
StringBuilder
。
Applicable scenarios
- Dynamic interpolation and readability priority scenarios.
6. Use
string[] words = { "Hello", "World" }; string result = (" ", words);
Features
- Used to merge string arrays or collections.
- Provides delimiter function, suitable for batch processing.
performance
- For arrays or collections, the performance is better.
- Internal optimization reduces unnecessary allocation.
Applicable scenarios
- Merge collections of strings (such as arrays, lists).
Performance comparison
From high to low (large-scale splicing scenario):StringBuilder > > > string interpolation ($) > > +/+=
-
Small scale splicing:
+
or$
Interpolate string. -
Medium-scale splicing:
or
。
-
Large-scale splicing or circular splicing:
StringBuilder
。
Summary of recommended usage scenarios
-
Code simplicity and readability are preferred:use
$
String interpolation or+
。 -
Need for high performance, especially in loops: Priority
StringBuilder
。 -
Collection merge:
。
When selecting a method, weigh performance and code readability and use it as needed.
The above is the detailed content of various implementation methods of string merging in C#. For more information about C# string merging, please pay attention to my other related articles!