C # Programming Basics-Strings

Source: Internet
Author: User

C # strings are declared using the string keyword and consist of one or more characters of a set of characters.

Concatenation string

Concatenating a string is the process of appending a string to the end of another string. Use the "+" or "+ =" operator to string the Fu Fu Wen of this or string constant. concatenated strings can also use string. Concat

Replace string

Use the Replace method to replace the specified Unicode character or all occurrences of a string with another specified Unicode character or string in the instance.

Comparing strings

When a string is compared, the result is a string greater than or less than a string, or two strings equal. The String class in C # provides the Compare method, the CompareTo method, the Equals method, the StartsWith method, and the EndsWith method for string comparisons.

Compare method: Used to compare two specified string objects and return an integer that indicates the relationship between the two in the sort order. Its basic format is as follows:

Compare (string,string)

Compare (String,string,boolean)

In the first format, if two parameter values are equal, 0 is returned, and if the first argument is greater than the second argument, 1 is returned, otherwise-1. In the second format, the parameter boolean is used to specify whether the case is ignored when two string objects are compared (true ignores case, false accepts case)

CompareTo method. The format is: String.CompareTo (String)

Compares this instance to a specified string object, returns 0 if the value of this instance is equal to the parameter value, or 1 if this instance is greater than the parameter value; otherwise returns-1.

The Equals method. Used to determine whether two string objects have the same value. The format is as follows: String. Equals (String). True if the value of this instance is the same as the value of the parameter, otherwise false.

StartsWith method. Used to determine whether the beginning of a string instance matches the specified string. The format is as follows: String. StartsWith (String), which returns true if the argument matches the beginning of the specified string, or false otherwise.

EndsWith method. Used to determine whether the end of a string instance matches the specified string. The format is as follows: String. EndsWith (String) that returns true if the argument matches the end of the specified string, otherwise false.

Splitting a string

C # uses the Split method to split a string into a series of strings according to the specified delimiter. The format is as follows: String. Split (char[])

Locating strings

The IndexOf method locates the index of the first occurrence in this instance of a character or substring in the string (the first character or substring index value is 0). The format is as follows: IndexOf (String). parameter is the character or substring to be positioned in the string. If the argument is null, the return value is 0, or 1 if the argument does not exist in the string, otherwise returns the index position of the argument at the string.

Extracting a string

The Substring method can extract a string of characters from a string.

Format one: Substring (INT32), retrieves the substring, starting at the specified character position.

Format two: Substring (INT32,INT32), retrieves a substring, starting at the specified character position and having the specified length.

Copy, delete, cut string

Copy string

Copying a string is the copy of a string into the target character object. Copy the string using the Copy method. The format is as follows: String. Copy (String)

Delete a string

Use the Remove method to delete a specified number of characters in a string.

Method One: Removes all characters from the specified position to the first position in the string. The format is as follows: Remove (Int32)

Method Two: Deletes the specified number of characters starting at the specified position in the string. The format is as follows: Remove (Int32,int32). The first parameter is the starting position of the deleted character, and the second parameter is the end position of the deleted character.

Cut string

Methods for clipping strings include trim method, TrimStart method, TrimEnd method

Trim method: Removes all leading and trailing occurrences of a specified set of characters from the current string. The format is as follows: String. Trim (), String. Trim (char[])

TrimStart method: Removes all leading occurrences of a set of characters specified in the array from the current string. The format is as follows: String. TrimStart (char[])

TrimEnd method: Removes all trailing occurrences of a set of characters specified in the array from the current string. The format is as follows: String. TrimEnd (char[])

Fill, insert, Case transform string

padding string

PadLeft method: Right-aligns the characters in a string and fills the string to the left with a space or a specified Unicode character to reach the specified total length. The format is as follows:

String. PadLeft (Int32) //parameter is the length of the string after the fill

String. PadLeft (Int32,char) ///The first parameter is the length of the filled string, the second argument is the character to be filled

PadRight method: Left-aligns the characters in the string, padding to the right of the string with a space or a specified Unicode character to reach the specified total length. The format is as follows:

String. PadRight (Int32) //parameter is the length of the string after the fill

String. PadRight (Int32,char)//The first parameter is the length of the filled string, the second argument is the character to be filled

Insert String

Inserts a specified string at the specified index in the string using the Insert method. The format is as follows: String. Insert (int32,string). The first parameter is the index position to insert (the index starts at 0), and the second argument is the string to insert.

Convert character case

You can convert a string to a larger or lower case by using the Toupper/tolower method. The format is as follows: String. ToUpper ()/string. ToLower ()

C # Programming Basics-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.