Connection to C # strings

Source: Internet
Author: User

1. Use the "+" (plus) Operator:
String str = "Hello" + "world";
Console. WriteLine (str);//Output "HelloWorld"

2. Use the Join () method:
The Join () method is a static method of the string class, meaning that string is the method of the class, not the method of the instance, and can be used directly.
The join method joins each element in a string array with the specified character as a delimiter.
String "" str1 = "Hello";
String "" STR2 = "World";
String. Join ("/", str1,str2);//Output "Hello/world", Connect as "/" as delimiter

3. Use the Concat () method:
Concat is also a static method of the string class, with multiple overloaded versions, which, if only one parameter is transmitted, returns the string if the argument is a string, or, if it is a non-string, the ToString () method of the appropriate type, which converts the argument to a string return.
If the descendants of multiple parameters, Concat to concatenate the strings together to return, if the descendants of the argument is not all string type, is not a parameter of the type of strings, call the corresponding ToString () method first converted to a string, and then connect back.

int intstr = 100;
String str = "Hello";
String. Concat (INTSTR,STR);//output "100Hello";
Since the parameter intstr of the descendant is an integer, not a string, before the result is returned, the ToString () method of the int type is called First, the INTSTR is converted to the string "100", then spliced, and finally the string "100Hello" is returned.

4. It is recommended to use Stringbuilde R if the string is longer or the number of connections isgreater.
StringBuilder Mystringbuilder = new StringBuilder ();
Mystringbuilder.append (yy);
Mystringbuilder.append (ZZ);
String yy = Mystringbuilder.tostring ();

If you are inserting statements in a build SQL statement, feel more practical with the join () method

Eg: When a book is added

Additional key codes are as follows:

First step: Receive page variables

String Book_type = this. Dropdownlist1.selectedvalue;
String bookname = This.book_name. Text.trim ();
String Price = This.price.Text.Trim ();
String write = This.writer.Text.Trim ();
String Kaiben = This.kaiben.Text.Trim ();
String Yinzhang = This.yingzhang.Text.Trim ();
String K=this. Dropdownlist2.selectedvalue;
String Zishu = This.text.Text.Trim ();
Zishu = Zishu + k;

String Version = This.version.Text.Trim ();
String Bookhao = This.bookhao.Text.Trim ();
String YingShu = This.yingshu.Text.Trim ();
String Jiejie = This.jiejie.Text.Trim ();
String FullName = this. Fileupload1.filename;
Response.Write (FullName + "<br>");
String filepath = "";

Step two: Build SQL statements

String temp = String.Join ("', '", Book_type, BookName, Price, write, Kaiben, Yinzhang, Zishu, version, Bookhao,yingshu, fil Epath, Jiejie);
String sql = "INSERT into Book table (type number, book name, Price, author, folio, sheet, number of words, edition, ISBN, Print, picture, Book Introduction) VALUES ('" +temp+ "')";

Join () When you try to use too many variables, the "+" connection is simple if less

Connection to C # strings

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.