Characters and strings

Source: Internet
Author: User

Character class

Char represents a Unicode character in C #, exactly the Unicode characters that make up a string. Unicode characters are a common character encoding for the current computer, which sets a uniform encoding for each character in different languages, and is used to meet the requirements of cross-language, cross-platform text conversion and processing.

Char ch1 = ' L ';

Common methods and descriptions for 1.Char classes:

Iscontrol: Whether it belongs to the control character category.

IsDigit: Whether it belongs to the decimal number category.

Ishighsurrogate: Whether it is a high surrogate.

Isletter: Whether it belongs to the letter category.

Isletterordigit: Whether it belongs to a letter or a decimal number category.

Islower: Whether it belongs to the lowercase letter category.

Islowsurrogate: Is the low surrogate.

Isnumber: Whether it belongs to a numeric category.

Ispunctuation: Whether it belongs to the punctuation category.

IsSeparator: Whether it belongs to the delimiter category.

Issurrogatepair: Whether to form surrogate pairs.

Issymbol: Whether it belongs to the symbol character category.

Isupper: Whether it belongs to the uppercase category.

Iswhitespace: Whether it belongs to a blank class.

Parse: Converts the value of the specified string to its equivalent Unicode character.

ToLower: Convert to lowercase.

Tolowerinvariant: Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.

ToString: Converts to a string.

ToUpper: Converts to uppercase.

Toupperinvariant: Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.

TryParse: Converts the value of the specified string to its equivalent Unicode character.

2. Escape character

The escape character \ (a single backslash) operates only on a single character immediately following it.

\ n: Carriage return for line break

\ t: jumps horizontally to the next tab position

\v: Vertical Jump Grid

\b: Backspace

\ r: Enter

\f: Page Change

\ \: Backslash character

\ ': single-quote character

The character represented by the \ddd:1-3 bit octal number

The character represented by the \xhh:1-2 bit hexadecimal

String class

1. Comparing strings

1). Compare method

Compares two strings for equality:

Int Compare (String stra, string StrB);

Int Compare (String stra, String StrB, bool ignorcase);

Note: Compare the position of the string in the English dictionary.

2). CompareTo method

Is the same as the instance object itself compared to the specified string.

Stra.compareto (StrB);

3). Equals method

The Equals method performs the order (case-sensitive and culture) comparisons.

Stra.equals (StrB);

String.Equals (Stra, StrB);

2. Formatting strings

public static string Fromat (string format, object obj);

Format: Used to specify the form in which the string is to be formatted.

OBJ: The object to be formatted.

For example:

String.Format ("{0},{1}!!!", Stra, StrB);

String.Format ("{0:d}", DT);

Format Specification Description

d Short Date format (YYYY-MM-DD)

D Full date format (yyyy mm month DD day)

T short time format (hh:mm)

T full time format (HH:MM:SS)

F Short Date/time format (yyyy mm month DD Day hh:mm)

F Complete Date/time format (yyyy mm DD day hh:mm:ss)

G Short sortable date/time format (YYYY-MM-DD hh:mm)

G complete sortable Date/time format (YYYY-MM-DD hh:mm:ss)

M/m Month/day format (mm month DD day)

Y/Y year/month format (yyyy mm month)

3. Intercepting strings

Stra.substring (1, 4);

4. Splitting a string

Public string[] Split (params char[] separator);

5. Inserting and populating strings

public string Insert (int startIndex, string value)

Public sting PadLeft (int totalWidth, char Paddingchar)

TotalWidth: Specifies the character length after padding

Paddingchar: Specifies the character to be populated and, if omitted, fills the space symbol

6. Delete a string

public string Remove (int startIndex);

public string Remove (int startIndex, int count);

7. Copying strings

public static string Copy (String str);

public void CopyTo (int sourceIndex, char[] destination, int destinationidex, int count);

Stra.copyto (1, str, 0, 4);

8. Replacing strings

public string Replace (char Ochar, char NChar);

public string Replace (string ovalue, string nvalue);

Sting stra = strb.replace ("Replace", "replace new")

StringBuilder class

The class represents a string-like object that has a value of variable character sequences. The value is variable because it can be modified by appending, removing, replacing, and inserting characters.

Common methods and descriptions in the 1.StringBuilder class:

Append: Appends text or a string to the end of the specified object.

Stringbuilder.append ("AAAA");

AppendFormat: Customizes the format of the variable and appends the values to the end of the StringBuilder object.

StringBuilder. AppendFormat ("{0:c}", Num);

Insert: Adds a string or object to the specified position in the current StringBuilder object.

StringBuilder. Insert (0, "name:");

Remove: Removes the specified number of characters from the current StringBuilder object.

StringBuilder. Remove (stringbuilder.length-15);

Replace: Replaces the characters within the StringBuilder object with another specified character.

StringBuilder. Replace ("Replace", "replace new");

The difference between a 2.StringBuilder class and a String class

If you want to manipulate a growing string, try not to use the string class instead of the StringBuilder class.

Two classes work differently: The String class is a traditional way of modifying a string, and it does work to add a string to another string. Yes, but in. NET Framework, this operation is really not a row. Because the system first writes two strings to memory, then deletes the original string object, creates a string object, and reads the data in memory to the object. This carefree matter, it took a lot of time. This is not the case with the StringBuilder class under the System.Text namespace, which provides a append method that can be used to modify the string in place of an existing object, simply and directly. Of course, the difference between these two efficiencies is not generally noticeable, but if you are going to do a lot of adding to a string, the time and string classes that the StringBuilder class consumes are simply not an order of magnitude.

"C # from Beginner to proficient" after reading summary

Characters and 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.