In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the splicing method of C # string". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the splicing method of C # string"!
First, directly through + splicing
Directly through + splicing is one of the most common ways in our code. Here is a simple code snippet to analyze and analyze.
1stringstr1 = "1"
two
3 stratum + "2"
The first piece of code, first allocates a memory space to store the str variable with a value of "1"
The second piece of code reallocates a new memory space to store "12" and points the str to the new address
Through the analysis, in fact, it is not difficult to find that the simple code at both ends will have two memory address operations, and the number of memory addresses allocated will increase with the number of concatenated strings. When several simple strings are spliced in this way, in fact, we still do not feel the impact on performance, but when the number of strings is large, you will feel it, which will not only cause a waste of memory. It also directly affects performance.
So in the actual development project, through + splicing strings is more common, but if you only see this way is not so friendly, since it is not friendly, then obviously there will be a more friendly way, then we will analyze and analyze the implementation of string stitching through StringBuilder.
Second, concatenate strings through StringBuilder
StringBuilder is actually an array of characters maintained internally, which can dynamically increase the length of its own data. Its default length is 16. When the stored string exceeds its length, it will automatically expand its length by 2 times.
Haha, speaking of which, I guess you can see the problem, that is, whether automatic capacity expansion beyond length also needs to sacrifice performance. Of course, you can't feel the impact on performance in several expansion times, but if you have too many words, you will feel very obvious. This is also some skills for using StringBuilder.
If we look at the code of different partners, you will find that when initializing the StringBuilder, the technician will initialize a length for the StringBuilder according to the estimated string size to be stored, which is the difference in detail.
After talking nonsense for a long time, is it the actual code to prove that it is not nonsense? No matter whether I am in a hurry, at the end of the article, I will write a comparative analysis of the test code.
Third, string.Format is no stranger, is it?
String.Format is also often seen for data stitching and filling in some formats, and one of its great advantages is that it looks clear.
In fact, when we look at the underlying implementation of string, we will find that its underlying nature is still implemented by StringBuilder.
Fourth, the $way to concatenate the string
String.Format 6.0 appeared $way to concatenate strings, in fact, to put it simply is the simplified operation version of string.Format, if there are too many strings stitched together, I may be confused about the corresponding relationship. I don't know if you have encountered it. Anyway, I have encountered it before. $is a good way to avoid the problem.
At this point, I believe that everyone on the "C # string splicing method" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.