Drill down into the String class in C #

Source: Internet
Author: User

Drill down into the String class in C #

1. String Lookup method
①.indexof (char/string value)
Find the location where the first match is located

②.lastindexof (char/string value)
Find the location where the last occurrence was found
* The return value of two methods is the int type, and if a match is found, the subscript is returned, and no-1 is returned;
* Two methods support char type and string type
③ Example:
int variable name = string. IndexOf ("Match");

2. Compatible case-sensitive form
①string. Trim ();
Remove whitespace at both ends of a string
②string. ToLower ();
Convert to lowercase
③string. ToUpper ();
Convert to uppercase

3.string Intercept String method
①.substring (int start position)
Intercept all strings starting from the starting position
* methods that use one parameter need to pass in the subscript position of the starting intercept character
②.substring (int start position, int intercept length)
2 parameters passed in: The first position represents the location where the intercept was started, and the second represents the intercept length
Two parameters are of type int

3.String Segmentation and linking
①. Splitting a string
Split () Method:
string[] str = string. Split (');
The return value is an array of type string
Parameters: Delimiter (char type)
* Because the. Split method splits a string, gets a character, and uses the array to receive the return value
② Link String
Join method:
String str = string. Join ("Parameter 1", Parameter 2)
The return value is a string
Parameter 1: Link character
Parameter 2: Array of strings
* Use String. The Join method links a string array to get the string

4. Comparing strings
① Comparison Size
Compare method:
String 1.Compare (String 2)
Compares the size of a two string, returning an integer. If STR1 is less than str2, the return value is less than 0.
If str1 equals str2, the return value equals 0. If STR1 is greater than str2, the return value is greater than 0.

② Comparing string Contents
Equals method:
String 1. Equals (String 2)
Comparison of two strings. If equal, the return value type is true. If not equal, the return value type is False

5. Tips

① operation "= =" and Equals () method
Operator "= =":
Compare the first address in memory of a two-valued object
Equals () Method:
Compares the values of two value objects for equality

② "" and string. Empty
"":
Assigning a zero-length storage space to a string object
String. Empty:
Represents an empty string and does not allocate storage space for a string


6. Formatting values
① Syntax:
String str = string. Format ("Formatting string", parameter list);

form of the ② format item
{index [, align] [: format string]}

③ Formatting a string table
Currency format: String. Format ("{0:C3}", 2000);
Decimal format: String. Format ("{0:d3}", 2000);
Fixed number of digits after decimal point: string. Format ("{0:f3}", 2000);
Separate numbers with commas: string. Format ("{0:n}", 2000);
Percent calculation method: String. Format ("{0:P3}", 2000);
Hexadecimal format: string. Format ("{0:x000}", 2000);

7. Conversion between a numeric type and a string

(1) Conversion
①parse () Method:
The data type. Parse (string);
② using the Convert class transformation:
Convert to an integer type (int type): Convert.ToInt32 (String).
Convert to single-precision float (float type): Convert.tosingle (String).
Convert to double-precision floating-point type (double type): Convert.todouble (String).
Convert to String type (String type): Convert.ToString (Strings).

* Relative security for data type conversions using the Convert class relative to the parse () method
(2) Convert to String type
① calls the ToString () method to implement the
Numeric type. ToString ();
Synchronizing in *java and C #

Drill down into the String class 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.