C # base class application--String processing class

Source: Internet
Author: User
String

First, introduce a few commonly used functions:

Compare (STR1,STR2)--Compare the size of the two string str1,str2, if greater than the return positive number, equal to return 0, less than return negative!

indexof--the position of the first occurrence of a given string in a positional string
PadLeft and padright--fills the string with the specified characters at the beginning and end of the string
ToLower and ToUpper convert strings to lowercase or uppercase
trim--Delete End to end blank
string.replace--replaces the specified character in the string with the specified character.


The process of establishing strings in C #:
For example, define variable strt= "Welcome to";
strt+= "www.sinory.com";
The program first creates an object of type System.String and initializes "Welcome to". At this point, the level of compilation allocates enough memory to hold the text string. This example is represented by a variable STRT. When the strt+= "www.sinory.com" is executed, a new instance is created and enough memory is allocated to hold the composite text. And then use the variable STRT to represent this new character.
strings, the string is undone. When you want to do large-scale character substitution and adjustment operations, using a string to operate will severely affect performance. In this case, you can use the System.Text.StringBuilder class.

The StringBuilder class does not have the capability of a string class to provide basic substitution and addition and deletion of text in a string, but it is highly efficient and can specify memory capacity when defining StringBuilder objects. If you do not specify the system, it is determined based on the length of the string when the object is initialized. It has two main parameters length and capacity representing the actual length of the string and the length of memory space occupied by the string. The modification of the string is done in this memory, greatly improving the efficiency of adding and replacing.
As defined: StringBuilder sb=new StringBuilder ("Hello,welcome", 100);//Initialize object and set initial capacity to 100
Sb. Append ("to www.sinory.com");
Sb. Replace (old,new);//replaces the old with new, just as string.replace () does not need to copy characters in the procedure.
Members of the StringBuilder:
StringBuilder sb=new StringBuilder ("www.sinory.com");//define an object with an initial value of www.sinory.com.
StringBuilder sb=new StringBuilder (20); Initializes an empty object with a capacity of 20.
In addition StringBuilder also has the Maxcapacity property to limit the maximum capacity that an object can use. The default is approximately int. MaxValue (2 billion)
You can define SB in the process of using. Maxcapacity=value;
Sb. Append () appends a string to the current string.
Sb. AppendFormat ()--adding strings in a specific format
Sb. Insert ()--Inserts a substring
Sb. Remove ()--delete character from current string
Sb. Replace ()--replaces the character specified in the string
Sb. ToString ()--converts SB to a string object




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.