C # Data structures and algorithms-strings, string classes, and StringBuilder classes

Source: Internet
Author: User
Tags array to string

* This article is for excerpt notes *

Preface
The StringBuilder class is used when a program needs to make many changes to a string object.
Because strings and string objects are immutable, StringBuilder objects are variable.
The string class is immutable, which means that each time an object is changed, a new copy of the object needs to be created.
If you are creating long strings, or making a lot of changes to the same objects, then you should use the StringBuilder class instead.
StringBuilder objects are mutable and performance is better.

A string is a sequence of characters that can contain letters, numbers, and other symbols. In the C # language, a literal string can be generated using double quotes "Yamin Wang".

The string can consist of any character from the Unicode character set.
A string can also be composed of no characters, and this particular string is called an empty string. (“”)
The string that represents the space is ("")

A string is both a local type and an object of a class. You can use the string as a local data value.

1 Creating a String Object
String Name= "Yamin Wang";
The C # string allows the escape character to be placed in it.
The escape character starts with a backslash (\) followed by a single letter representing the layout.

2 methods for commonly used string classes
The three most important operations are: 1) find the substring of String 2) to determine the length of the string 3) to determine the position of the character in the string.

String. Length determines how long a string is
String. IndexOf ("") determines the position of the character in the string
String. Substring (start position, number of characters to be extracted) find substring of string

3 Split method and join method
Split method: Used to decompose a string. After the split method takes a string, it breaks it down into a data component block, and then puts the blocks inside the string array.
When you use the Split method, you can specify the contents of the delimiter to look for.
The parameter must appear as an array of type char. The first element of the array will be the character used as the delimiter.
Data. Split (delimiter, the number of elements to store in the array)
e.g.
String[] sdata;
char[] delimiter=new char[]{', '};
Sdata=data. Split (Delimiter,data. Length);

Join method: Changes from array to string. This method takes two parameters: the original array and the characters used to separate the elements.
Call this method from the string class itself instead of the instance of string.
String.Join ("character", array)

4 Ways to compare strings
Equals method: Called by a String object and takes another string object as its argument. Compares two string objects by character.
If same, returns True,else return False
e.g.
S1. Equals (S2)

CompareTo method: Takes a string as a parameter. will return 1,-1,0.
e.g.
S1.compareto (S2)

If S1==s2 returns 0
S1>S2 return-1;
S2>S1 returns 1;

Compare method: Use a class method to invoke the
String.Compare (S1,S2)

StartsWith
EndsWith: Takes a string as an argument, if the instance starts or ends with a string argument
Then the method will return true

e.g.
noun. EndsWith ("s")

5 Methods for handling strings
Insert new character, remove character from string, replace old character with new character, change some characters
As well as adding spaces to a string or removing spaces from a character. There are appropriate methods for these operations in the string class.

Insert:
String1=string0.insert (position,string)
Remove
(Take two integer parameters, a starting position and a counter, which is the number of characters to remove)
S1=s1. Remove (Position,name. Length)
Replace
Take two parameters: the string to remove and the string to replace. Returns a new string.

ToLower
ToUpper Convert a string from small to upper case

Trim
TrimEnd Remove a space or other character from either end of a string

Arr. Trim (Chararr[0])


StringBuilder class
The StringBuilder class provides access to a variable string object. The object of the string class is immutable, and each time the value of the string object is changed, a new object is created to hold the value.
The StringBuilder object is changeable. When changes are made to the StringBuilder object, the original object is being changed rather than the copy operation.

Using the StringBuilder class must use System.Text
1. Building StringBuilder objects
StringBuilder sb=new StringBuilder (); The default is to save a string of 16 characters long
StringBuilder sb=new StringBuilder (25); A 25-character object can be saved.
StringBuilder sb=new StringBuilder ("Hello,world")
Each time the capacity of the StringBuilder object is exceeded, the capacity increases by 16 characters.

2. Get and set information about the StringBuilder object
length specifies the number of characters in the current instance
The Capacity property returns the current capacity of the instance capacity ()
Maxcapacity returns the maximum number of characters allowed in the current instance of an object

3. Modify the StringBuilder object
Append method: Adds a character at the end of the StringBuilder object.
Takes a string as an argument and concatenates the string to the end of the current value in the object.
Sb. Append (Words[i])

Insert: Allows you to insert a string into the current StringBuilder object.
Take three parameters, the first one indicates the starting position of the insertion startposition, and the second is the string to be inserted. The third parameter is optional, indicating the number of times you intend to insert a string into the object.

Remove removes the character from the StringBuilder object. Two parameters: The starting position and the number of characters to remove. ‘
Replace: Replaces the character of the StringBuilder object. Two parameters: The old string to replace and the new string to replace.

When working with StringBuilder objects, it is often necessary to convert them into strings, which can be implemented using the ToString method. Returns a string instance in the StringBuilder instance.

C # Data structures and algorithms-strings, string classes, and StringBuilder classes

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.