Summary of characters and strings in C #

Source: Internet
Author: User
Tags uppercase letter

The Char class is the character type provided by C #, which is the string type provided by C #.

Character:

The Char class represents a Unicode character in C #.

The Char class defines only one Unicode character.

The common methods and descriptions of the Char class are as follows:

Method

Description

Iscontrol

Indicates whether the specified Unicode character belongs to the control character category

IsDigit

Indicates whether a Unicode character is part of a decimal number category

Ishighsurrogate

Indicates whether the specified Char object is a high surrogate

Isletter

indicates that a Unicode whether the character belongs to the letter category

Isletterordigit

indicates that a Unicode Whether the character belongs to an alphabetic category or a decimal number category

Islower

indicates that a Unicode whether the character belongs to a lowercase letter category

Islowsurrogate

Indicates whether the specified Char object is a low surrogate

Isnumber

indicates that a Unicode whether the character belongs to a numeric category

Ispunctuation

indicates that a Unicode whether the character belongs to the punctuation category

IsSeparator

indicates that a Unicode whether the character belongs to a delimiter category

Issurrogate

Indicates whether a Unicode character belongs to the surrogate character category

Issurrogatepair

Indicates whether the two specified char objects form a surrogate pair

Issymbol

indicates that a Unicode whether the character belongs to the symbol character category

Isupper

indicates that a Unicode whether the character belongs to the uppercase category

Iswhitespace

Indicates whether a Unicode character belongs to a blank category

Parse

Converts the value of the specified string to its equivalent Unicode character

ToLower

Converts the value of a Unicode character to its lowercase equivalent

Tolowerinvariant

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

Tostring

Converts the value of this instance to its equivalent string representation

ToUpper

the Unicode converts the value of a character to its uppercase equivalent

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

You can see that char provides a lot of practical methods, where it is more important to start with IS and to. Most of the methods that begin with is determine if the Unicode character is a category, and the method that starts with to will be converted primarily to other Unicode characters.

Example 001 use of the Char class

Create a console application that shows how to use the common methods provided by the Char class, as shown in the code below.

Escape character

C # takes the character "\" as an escape character. For example, a character is defined, and the character is a single quotation mark, and an error is generated if the escape character is not used.

The escape character is equivalent to a power converter, the power converter is to obtain the required form of power through certain means, such as AC into DC, high voltage to low voltage, low frequency to high frequency. An escape character is also a combination of characters that convert a character to another form of operation or that cannot be used together.

Attention:

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

1 Static voidMain (string[] args)2 3 {4 5     CharA ='a';//declaring character a6 7     Charb ='8';//declaring character B8 9     Charc ='L';//declaration character CTen  One     CharD ='.';//declaring character D A  -     CharE ='|';//declaration character E -  the     Charf =' ';//declaration character F -  -     //use the Isletter method to determine whether a is a letter -  +Console.WriteLine ("Isletter method to determine if a is a letter: {0}", Char.isletter (a)); -  +     //use the IsDigit method to determine whether B is a number A  atConsole.WriteLine ("IsDigit method to determine whether B is a number: {0}", Char.isdigit (b)); -  -     //use the Isletterordigit method to determine whether C is a letter or a number -  -Console.WriteLine ("Isletterordigit method to determine whether C is a letter or a number: {0}", Char.isletterordigit (c)); -  in     //use the Islower method to determine if a is a lowercase letter -  toConsole.WriteLine ("Islower method to determine if a is lowercase: {0}", Char.islower (a)); +  -     //use the Isupper method to determine if C is an uppercase letter the  *Console.WriteLine ("Isupper method to determine if C is an uppercase letter: {0}", Char.isupper (c)); $ Panax Notoginseng     //use the Ispunctuation method to determine if D is a punctuation mark -  theConsole.WriteLine ("Ispunctuation method to determine if D is a punctuation mark: {0}", Char.ispunctuation (d)); +  A     //use the IsSeparator method to determine if E is a delimiter the  +Console.WriteLine ("The IsSeparator method determines whether E is a delimiter: {0}", Char.isseparator (e)); -  $     //use the Iswhitespace method to determine if f is blank $  -Console.WriteLine ("Iswhitespace method to determine if F is blank: {0}", Char.iswhitespace (f)); -  the console.readline (); - Wuyi}
View Code

Description: Most of the important regular expression language operators are non-escaping single characters. Escape character \ (single backslash) notifies the regular expression parser that the characters following the backslash are not operators. For example, the parser treats R as a character, and the backslash (\ r) followed by R is considered a carriage return function.

To avoid escape sequence element escapes, there are two ways to avoid this.

1, through the @ character implementation.

2, by verbatim specifying the string literal value (two backslash) implementation.

String:

First understand the concepts and differences of String,string,stringbuilder:

String

The explanation given by STRING,MSDN is that the string is a keyword in C # and a reference type, and the string type represents a sequence of 0 or more Unicode characters. String is the alias of a string in the. NET Framework. But the Define equality operator (= = and! =) is to compare the value of a string object (rather than a reference) (which is explained later in the example).

String:

A string is a class that represents text, which is a series of Unicode characters. The String object is immutable. Each time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new space to be allocated for the new object. For example: When we instantiate a string object, we allocate a space in memory for this object. as follows: String str = "Hello"; when we modify the value of STR, such as str = "Hello World", the system will reassign a space for Str. The original memory space is wasted and can only wait for the garbage collector to reclaim it. The overhead associated with creating a new string object can be very expensive in situations where you need to perform repeated modifications to the string.

The difference between string and string:

String is an alias for string in the. NET Framework, and string is a C # primitive type (primitive), which is simply the data type that the compiler supports directly. Primitive types are mapped directly to types in the Framework class library (FCL), for example, a primitive type int in C # is mapped directly to the System.Int32 type, where int is the primitive type, and System.Int32 is the FCL type. String is an FCL type, so when C # is compiled, the string is automatically converted to System.String. So string is essentially no different from string, except that a string is used to convert to string. Therefore, we recommend the use of string when coding.

String is a reference type, but (= = and! =) is a value that compares a string object (not a reference).

Overview:

When you use the String class, all the methods that face the ability to modify a string are not actually modifiable, they actually return a new string object that is modified based on the method being called, and you can use the StringBuilder class if you want to modify the actual contents of a string string.

Various string manipulation methods:

    1. Comparison:

(1) Compare:

Int Compare (String stra,string Sreb)

Int Compare (String stra,string sreb,bool ingorcase)

Note: If Ingorcase is true then the case is ignored.

Return value: A 32-bit signed integer.

(2) CompareTo:

(compares the instance object itself to the specified string)

public int CompareTo (string strB)

Return value: A 32-bit signed integer.

(3) Equals

public bool Equals (string value)

public static bool Equals (string a,string b)

2. formatting strings

public static string Format (String Format,object obj)

Format: The specified formatting

OBJ: the object being formatted

Format characters

Associated Properties/Description

D

ShortDatePattern

D

Longdatepattern

F

Full date and time (long date and short time)

F

Fulldatetimepattern (long date and long time)

G

General (short date and short time)

G

General (short date and long time)

Mm

Monthdaypattern

R, R

Fc1123pattern

S

Use of local time

T

Shorttimepattern

T

Longtimepattern

U

Universalsortabledatetimepattern

U

Full date and time with Universal Time (long date and long time)

Y, y

Yearmonthpattern

3. Intercept string

public string Substring (int startindex,int length)

StartIndex: The index of the starting position of the substring

Length: The number of characters in a substring

4. Split string

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

Separator: An array containing delimiters

Return value: An array whose elements contain substrings in this instance, separated by one or more characters in separator.

5. Inserting and populating strings

Insert string:

public string Insert (int startIndex, string value);

Padding string:

public string PadLeft (int totalwidth,char paddingchar);

TotalWidth: The number of characters in the resulting string, equal to the number of original characters plus any other padding characters

Paddingchar: Fill character

return value left Justified

public string padright (int totalwidth,char paddingchar);

The return value is right justified

To copy a string:

Copy:public static string Copy (String str);

copyto:public void CopyTo (int sourceindex,char[]destination,int destinationindex,int count);

SourceIndex: The starting position where the string needs to be copied

Destination: array of target characters

Destinationindex: Specifies the starting position in the target array

Count: Specifies the number of characters to copy

Replacement string:

public string Replace (char Ochar,char NChar);

public string Replace (string ovalue,string nvalue);

Ochar: Characters to be replaced

NChar new characters after replacement

To delete a string:

public string Remove (int startIndex);

public string Remove (int startindex,int count);

Variable string:

public StringBuilder ();

public StringBuilder (int capacity);

Public StringBuilder (string value);

Public StringBuilder (int capacity,int maxcapacity);

Public StringBuilder (string value,int capacity);

Public StringBuilder (String value,int startindex,int length,int capacity);

Recommended start and end size for Capacity:stringbuilder objects

Value: String that contains the substring used to initialize the StringBuilder object

Maxcapacity: The maximum number of characters that the current string can contain

StartIndex: Starting position

Length: The number of characters in a string

1. The Append method can be used to add a string representation of text or an object to the end of a string represented by the current StringBuilder object.

2. The AppendFormat method adds text to the end of the StringBuilder and implements the IFormattable interface, so the standard format string described in the formatted section can be accepted.

3. The Insert method adds a string or object to the specified position in the current StringBuilder.

4. You can use the remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index.

5. Using the Replace method, you can replace the characters within the StringBuilder object with another specified character.

Summary of characters and strings in C #

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.