C # Programming (10_ concatenation of multiple strings)

Source: Internet
Author: User

Concatenation is the process of appending a string to the end of another string. + operator, the compiler creates a single string. "Data-guid=" 7b08363269c65c3e363f0442f018a420 " > When you use the + operator to concatenate string literals or string constants, the compiler creates a string. Concatenation does not occur at run time. But string variables can only be concatenated at run time, for this, You should understand the performance implications of various methods.

The following example shows how to split a long string into several shorter strings, which improves the readability of the source code. these shorter strings are concatenated into a string at compile time. There is no runtime performance overhead, regardless of the number of strings involved.

Static voidMain () {//concatenation of literals is performed at compile time, not run time.    stringText ="Historically, the world's data and the world of objects"+"There is not a been well integrated. Programmers work in C # or Visual Basic"+"And also in SQL or XQuery. On the one side is concepts such as classes,"+"objects, fields, inheritance, and. NET Framework APIs. on the other side"+"is tables, columns, rows, nodes, and separate languages for dealing with"+"them. Data types often require translation between the worlds; There is"+"different standard functions. Because the object world have no notion of query, a"+"query can only be represented as a string without compile-time type checking or"+"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to"+"objects in memory is often tedious and error-prone."; Console.WriteLine (text);}

To concatenate string variables, you can use the+ or + = operator, you can also use String. Concat,String. Format or StringBuilder. Append method. The + operator is easy to use and helps to improve the visual nature of the code. Even if multiple + operators are used in a single statement, the contents of the string are copied only once. However, if you repeat this operation multiple times, such as using loops, you can cause efficiency problems. For example, consider the following code:

Static voidMain (string[] args) {    //to run the provide a command line string. //in Visual Studio, see Project > Properties > Debug.    stringUserName = args[0]; stringDate =DateTime.Today.ToShortDateString (); //Use the + and + = operators for one-time concatenations.    stringstr ="Hello"+ UserName +". Today is"+ Date +".";    System.Console.WriteLine (str); STR+="How is you today?";    System.Console.WriteLine (str); //Keep the console window open in debug mode.Console.WriteLine ("Press any key to exit."); Console.readkey ();}//Example Output://Hello Alexander. Today is 1/22/2008.//Hello Alexander. Today is 1/22/2008. How is you today?//Press any key to exit.//

!--? Xml:namespace PREFIX = "[Default] http://www.w3.org/1999/xhtml" NS = "http://www.w3.org/1999/xhtml"/--> If the number of strings you concatenate is not so large (for example, in a loop), then the performance cost of the code may not be very high. string. Concat and string . Format methods. "Data-guid=" 2b232fcd8fb88ae8a5dc260ada087d48 ">string . Concat and string . Format methods. "Data-guid=" 2b232fcd8fb88ae8a5dc260ada087d48 "the same applies to String . Concat and String . The Format method.  

However, if performance is important, you should always use the StringBuilder class to concatenate strings. the following code uses the Append method of the StringBuilder class to concatenate strings, so there is no link action for the + operator.

classstringbuildertest{Static voidMain () {stringText =NULL; //Use StringBuilder to concatenation in tight loops.System.Text.StringBuilder SB =NewSystem.Text.StringBuilder ();  for(inti =0; I < -; i++) {sb.        Appendline (i.ToString ()); } System.Console.WriteLine (sb.)        ToString ()); //Keep the console window open in debug mode.System.Console.WriteLine ("Press any key to exit.");    System.Console.ReadKey (); }}//Output://0//1//2//3//4// ...//

From the MSDN.

C # Programming (10_ concatenation of multiple strings)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.