s1/c# Language and database technology fundamentals/04-in-depth C # string class

Source: Internet
Author: User

Common string processing methods

Method

Description

Bool Equals (String value)

Compares the value of a string with another string of values for equality. Returns true if they are equal, or false if not equal

Int Compare (String stra,string StrB)

Compares the size relationship of two strings and returns an integer. If Stra is less than StrB, the return value is less than 0, and if Stra equals StrB, the return value is 0; if stra is greater than StrB, the return value is greater than 0

Int IndexOf (char, String value)

Gets the position of the first occurrence of the specified value string in the current string. If value is found, it returns to its position; 1 if not found

Int LastIndexOf (String value)

Gets the position of the last occurrence of the specified string value in the current string. If value is found, the position is returned, and if it is not found, it returns -1

String join (String separator,string[] value)

connects each string in the string array value with the specified delimiter separator , returning the concatenated string

String[] Split (char separator)

Separator the split string with the specified delimiter , returning the segmented string array

String Substring (int startindex,int length)

retrieves the length of a string starting at the specified location StartIndex

String ToLower ()

Get the lowercase form of a string

String ToUpper ()

Get the uppercase form of a string

String Trim ()

Remove extraneous spaces at both ends of the string

Tips:

1, the operation "= =" and the Equals() method:

The operator "= =" and the Equals() method are applied to two strings, and the content is judged to be different. Simply put,"= =" determines the first address in memory of two string objects, that is, whether it is the same string object, and the Equals() method determines whether the values of two string objects are equal.

2, "" and String.Empty:

"": assigns a zero-length storage space to the String object.

String.Empty: Represents an empty string and does not allocate storage space for the object.

In C # , in most cases, "" and String.Empty can be used interchangeably, and the latter is generally recommended.

Connecting a split string

Note: theJoin () method is a static method of the string class and is called with the string class name, which differs from the Split () method.

Information:

A "\ n" symbol appears in the output statement . This is a special character constant, and it features the following.

1 . Start with a backslash "\" followed by one or several characters.

2, has the specific meaning, differs from the character original meaning, therefore is called "the escape character ".

3, mainly used to denote those with the general character is not easy to represent the control code, with the visible characters to represent the invisible characters. For example, "\ n" indicates a newline character, and"\ T" indicates a horizontal tab.

The correct code for the C # set path is "C:\\Temp", where "\ \" is an escape character that represents a backslash. C # provides the @ character to ignore the escape character in order to avoid the programmer being unable to find the path in the program execution because of a missing "\" symbol. For example, using the @ character simplifies setting the path to the code @ "C:\temp".

format Formatting

String Name= "Wang Qiang ";

Console.WriteLine (" My name is {0}, my age is {1}.") ", name,18);

In this code, theparameter of the Console.WriteLine () method " My name is {0}, my age is {1}." called the format string, the section after the format string, "name," is the parameter list, and {x} in the format string is called a placeholder.

The format() method allows you to insert a string, number, or Boolean variable into a format string, and its syntax is similar to the WriteLine() method.

Grammar:

String Mystring=string.format (" format string ", parameter list );

Where the format string contains fixed text and format items. The format item is in the form shown below.

{ index [, alignment ] [ : Format string ]}

Where the index starts at 0, corresponds to the list of variables, the alignment section sets the width and alignment of the display, is a signed integer, the size of the integer represents the width of the data, the positive number is right, the negative number is left-justified, and the format string section contains the format specifier.

For example:

String Mystring=string.format ("{0} multiplied by {1} equals {2}", 2, 3, 2 * 3);

Formatting Numeric Results table

Character

Description

Example

Output results

C

Currency format

String.Format ("{0:C3}", 2000)

¥2 000.000

D

Decimal format

String.Format ("{0:d3}", 2000)

2000

F

Fixed number of digits after decimal point

String.Format ("{0:f3}", 2000)

2000.000

N

numbers separated by commas (,)

String.Format ("{0:n}", 250000)

250,000

P

Percent Count method

String.Format ("{0:p3}", 0.29768)

29.768

X

hexadecimal format

String.Format ("{0:x000}", 12)

C

For example:

String.Format ("{0,-8:f2}", 23);

The result is "23.00", that is, left- aligned, Width 8, and 2 -bit decimals are output .

String.Format ("{0,8:c2}", 23);

The results are displayed as "¥23.00", that is, right- aligned, with a width of 8, the currency symbol is displayed, and 2 decimal places are retained to output the value .

Type conversions

1. Implicit type conversion

For any numeric type A, you can implicitly convert to type B as long as its range of values is fully contained within the range of the value of type B . That is, theint type can be implicitly converted to float or double , and thefloat type can be implicitly converted to type double.

2. Display Type conversion

Converts a type with a large range of values to a type with a small range of values.

Conversion between a numeric type and a string

1. String conversion to numeric type

When you want to convert a string to a numeric type, you can call the parse () method of a different numeric type (theChinese meaning of parse is parsing).

For example, to convert a string to an integer type of code:

Int. Parse (string);

To convert a string to a single-precision floating-point code:

Float. Parse (string);

To convert a string to a double-precision floating-point code:

Double. Parse (string);

Note that the string to be converted must be a valid representation of the number, that is, the corresponding number on the surface.

2. Convert numeric to String

Strings can be converted to numeric values, so how to convert data of numeric types to strings, as in Java C # as long as you call the ToString () method. For example:

int age = 18;

String Myage=age. ToString (); Ww

converting using the Convert class

The Parse () method converts a string type into a numeric type, but there is a more versatile class--convert class in C # that can perform the conversion of data types between the various base types.

type conversion methods for commonly used convert classes

Method

Description /p>

convert.toint32 ()

convert to int (Int type )

convert.tosingle ()

Convert to single-precision floating-point (Float Type )

convert.todouble ()

/td>

(Double type

convert.tostring ()

(String)

From the results can be seen: converted to int , the rounding calculation, this and the display type conversion is different, before we use the display type conversion to convert 58.5 to int is the result of The value after the decimal point is discarded directly.

Summary: The range of methods used for various conversions

Implicit type conversions: often used between numeric types, converting a numeric type with a small range of values to a numeric type with a large range of values.

Display Type conversions: Commonly used between numeric types, converts a numeric type with a large range of values to a numeric type that has a small range of values, and uses the (XXX) variable or object.

Parse () method: Converts a string to another type, Yong FA wai xxx. Parse (String).

Convert class: The conversion of any basic type to another.

It is important to note that when converting using the Parse () method and The Convert class, there may be an error if the conversion is meaningless.

s1/c# Language and database technology fundamentals/04-in-depth C # string class

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.