10-17c# part fourth-type (1)

Source: Internet
Author: User
Tags truncated

C # Type--string class

One, String type ()

Note: string is different from string: string is a data type of string, lowercase string is an instantiation of uppercase string, string is string, so property with string.

Example: String A; The variable that initializes and instantiates a variable is called an object, for example: string.

String:

1. Properties of String : (no parentheses are needed later)

1). Length, which is the//.length of the string, is the method of manipulating the string type, which is to get the length of the string, return the value of type int, and require the int type to receive.

String S=console.readline ();

int i = s.length;

Console.Write (i);//NOTE: Returns the number of int numeric type

After execution:

2, String method (the type must be appended with parentheses, that is, with the condition)

1). TrimStart (); (truncate the opening space)//.  TrimStart (); only the whitespace at the beginning of the string can be removed, and the middle and trailing spaces cannot be executed.

String S=console.readline ();

S= S.trimstart ();

Console.Write (s);//Note: A new string that removes the opening space is replaced with a string that originally had a space, so s is re-assigned;

After execution:

2). TrimEnd () (truncated trailing space)//. TrimEnd (); Remove the space after the string

String S=console.readline ();

s = s.trimend ();

Console.Write (s);//The output is still a string, but it is a new string that is re-assigned

After execution:

3). Trim ();//Returns a string type that removes the space at the beginning of the string, but the intervening space is not removed.

String S=console.readline ();

s = S.trim ();

Console.Write (s);

After execution:

4) ToUpper (); (all uppercase)//The lowercase letter in the string into uppercase letters, example: verification code.

String S=console.readline ();

s = S.toupper ();

Console.Write (s);

After execution:

5). ToLower (); (all lowercase)//convert the lowercase letters inside the string to uppercase

String S=console.readline ();

s = S.tolower ();

Console.Write (s);

After execution:

6).  Substring (M,n); Start index starts with the string in the number of strings, the first character is the No. 0 bit (starting from 0), the string is truncated from the left of the first m, and the N

String S=console.readline ();

S=s.substring (M,n); //Note: The meaning in parentheses is that the first character is the No. 0 digit, which is the number starting from 0, the string is truncated from the left of the M character, and N is intercepted;

Console.Write (s);//The output is still a string type;

Examples:

Exercises:

7). Replace ("M", "n"), n instead of M ()//Replace all strings that match the specified string condition, returning a new string where all specified strings are replaced with another string

String S=console.readline ();

S=s.replace ("word", "child");//Replace the preceding character with the following character;

Console.Write (s);

After execution:

8). IndexOf ();//index, index; the position of the first occurrence of the specified string is the number of

String S=console.readline ();

int D=s.indexof ("ab");

Console.Write (d);//Although a string is specified, the output is the number of bits of the position where the string first appears, and is the value of type int

After execution:

9). LastIndexOf ();//Returns the value of type int, the position of the last occurrence of the character specified in the string;

String S=console.readline ();

int d = S.lastindexof ("a");

Console.Write (d);//

After execution:

10). StartsWith (); (compares the beginning string)//is received with a bool value, determines whether the string begins with the specified string, if it is true;

string s = Console.ReadLine ();

BOOL B = S.startswith ("123");

Console.Write (b);

Console.ReadLine ();

After execution:

11). EndsWith (); (vs. end string)//Determine if the end is this value, return the BOOL value

string s = Console.ReadLine ();

bool F = s.endswith ("123");

Console.Write (f);

After execution;

12). Contains ();//Determines whether the original string contains the specified character segment and returns a bool value;

string s = Console.ReadLine ();

BOOL E=s.contains ("abc");

Console.Write (e);

After execution:

13). ToString ("#.##");//When there are decimals, rounding takes 2 digits after the decimal point, and when int, the integer is not appended. 00;

Double i = double. Parse (s);

s = i.tostring ("#.##");

Console.Write (s);

After execution:

Contains fractional parts:

Only the integer part, not Add. 00 (with the following. ToString ("#,#.##");)

14). ToString ("#.00"), or//if there are decimals, rounding takes 2 digits after the decimal point, and when int, Integer plus. 00

Double i = double. Parse (s);

s = i.tostring ("#.00");

Console.Write (s);

After execution:

Contains fractional parts: rounded to 2 digits after decimal point

Only the integer part: Plus. 00

15). ToString ("#,#.00");//integer part starting from the single digit, from right to left, three bits separated by commas, rounded to 2 digits after the decimal point, when int, plus. 00

Double i=double. Parse (s);

s = i.tostring ("#,#.00");

Console.Write (s);

Console.ReadLine ();

After execution:

Contains fractional parts: rounded to 2 digits after decimal point

Only the integer part: Plus. 00

16). ToString ("#,#.##");//integer part starting from the single digit, from right to left, three bits separated by commas, rounded to 2 digits after the decimal point, when int, not add. 00;

Double i=double. Parse (s);

s = i.tostring ("#,#.##");

Console.Write (s);

Console.ReadLine ();

After execution:

Contains fractional parts: rounded to 2 digits after decimal point

Only the integer part: No. 00;

17). ToString ("#,#");//integer part starting from digit, from right to left, three bits separated, plus comma

Double i=double. Parse (s);

s = i.tostring ("#,#");

Console.Write (s);

Console.ReadLine ();

After execution:

These are some of the properties contained in this large class of string (that is, what it does) and methods (that is, the fixed usage that this program uses), which requires remembering each property or method, which returns a value type, a Boolean, or a string type, in order to be proficient in the application to the console program.

10-17c# part fourth-type (1)

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.