C # basic applications -- string processing

Source: Internet
Author: User


First, we will introduce several common functions:
Compare (str1, str2) -- Compare the size of two strings str1 and str2. If it is greater than the return positive number, it is equal to the return 0. If it is less than the return negative number!
IndexOf -- locate the position where a given string appears for the first time in a string
PadLeft and PadRight -- fill the string with the specified character at the beginning and end of the string
ToLower and ToUpper convert strings to lowercase or uppercase
Trim -- delete blank spaces at the beginning and end
String. Replace -- Replace the specified character in the String with the specified character.
C # string creation process:
For example, define the variable strT = "Welcome ";
StrT + = "www.sinory.com ";
The program first creates a System. String type object and initializes it as "Welcome ". At this time, the compilation level will allocate enough memory to save the text string. Use the strT variable to represent this instance. When strT + = "www.sinory.com" is executed, the system creates a new instance and allocates enough memory to save the composite text. Then, the variable strT is used to represent the new character.
String. When you want to perform large-scale character replacement and adjustment operations, using strings will seriously affect the performance. In this case, the System. Text. StringBuilder class can be used.

The StringBuilder class does not have the powerful functions of the String class. It only provides basic replacement and Addition and deletion of text in strings, but it is very efficient, when defining a StringBuilder object, you can specify the memory capacity. If no value is specified, the system determines the size based on the string length during object initialization. The parameter Length and Capacity indicate the actual Length of the string and the memory occupied by the string respectively. Modifying strings is performed in this memory, which greatly improves the efficiency of adding and replacing strings.
For example, define StringBuilder sb = new StringBuilder ("Hello, Welcome", 100); // initialize the object and set the initial capacity to 100
Sb. Append ("to www.sinory.com ");
Sb. Replace (old, new); // Replace old with new, which is the same as String. Replace () but does not need to be copied during the process.

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.