String (1), string (

Source: Internet
Author: User

String (1), string (


String

Famous string variable string myString = "first string ";
Display string Console. WriteLine ("myString =" + myString );

When you want to perform large-scale character replacement and adjustment operations, using the String class will seriously affect the performance. In this case, you can use the System. Text. StringBuilder class. The StringBuilder class does not have the powerful functions of the String class. It only provides the basic function of replacing, adding, and deleting text in strings, but it is very efficient.

Sb. Length indicates the actual Length of the string.
Sb. Capacity indicates the memory space occupied by the string
Sb. Append () Append the string to the current string
Sb. AppendFormat () add strings in a specific format
Sb. Insert () Insert a substring
Sb. Remove () Remove characters from the current string
Sb. Replace () replaces the string specified in the string
Sb. ToString () converts sb to a String object

Connection string

Use the Concat () method
For example, string myString1 = string. Concat ("hello,", "friend ");
Console. WriteLine ("String. Concat (\" Friend, \ ", \" Hello \ ") =" + myString1 );
// String. Concat ("friend,", "hello") = Hello, friend

Use + operator
For example, string myString1 = "hello," + "friend ";
Console. WriteLine ("\" Friend, \ "+ \" Hello \ "=" + myString1 );
// "Hello," + "friend" = Hello, friend

Use the jion () method
String [] myStrings = {"you", "good "};
String myString = String. jion ("-", myStrings );
ConSole. WriteLine ("myString =" + myString );
// You-OK

Evaluate string substrings

When we need to obtain a SubString from a String, we can use the SubString () method of the String class, which returns a SubString of the specified length starting from the specified position.

The SubString () method of the String class has two reloads:
SubString (int startIndex); returns the SubString from startIndex to the end of the string.
SubString (int startIndex, int lenght); returns a child string with a length of lenght starting from startIndex.
String myString = "www.fecit.com.cn ";
String myString1 = myString. SubString (3 );
Console. WriteLine ("String. SubString (3) =" + myString1 );
// String. SubString (3) = .fecit.com.cn
String mystring2 = myString. SubString (3,2 );
Console. WriteLine ("String. SubString (3,2) =" + myString2); // String. SubString (3,2) =. f

Delete character

To delete a specific character from a string, you can use the Remove () method.
The Remove () method of the String class has two reloads:
Public string Remove (int startIndex); returns the string from startIndex to the end of the string.
Public string Remove (int startIndex, int count );
The parameter startIndex is used to specify the location where the deletion starts, starting from 0 and count to specify the number of characters to be deleted.

A space is also a character.

String MyString = "Hello UnderWorld ";
Console. WriteLine (MyString. Remove (7,5 ));
// Hello World

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.