A detailed overview of common string methods in C #

Source: Internet
Author: User
Tags string methods
1) Intercept string using the substring method ,

The method has two overloaded functions in C #: substring (parameter), substring (parameter 1, parameter 2),

Use the following:

String A = "I ' m A string";
String b=a.substring (1);
String c=a.substring (1,6);

Where the passed parameter 1 is the starting position of the string, character string B intercepts all the characters after the 2nd character of the string A. The character string, C, will intercept strings with a length of 6 after the 2nd character of string A. The parameter must be greater than or equal to 0, A argumentOutOfRange exception is thrown if less than 0.

2) Convert string to character array
First, the string type variables can be thought of as a read-only group of char variables, so that each character can be accessed using the following syntax:

String A = "I ' m A string" char B =a[1];

If you change the string to a writable char array, you can use the ToChar Array () Method:

char [] = A.tochararray ();

Gets the length of the string using B.length.

3) Convert case
<string>. ToLower () Convert to lowercase
<string>. ToUpper () converted to uppercase

4) Delete the space of the string or the specified character
Remove spaces before and after a string:

<string>. Trim ()

Delete the specified character:
Specify a specific character with a char array first

Char[] C ={", ' e ',}<string>. Trim (C)

You can also use TrimStart (), TrimEnd () to remove spaces before or after the specified character

5) Add a space or a specified character before or after the string
<string>. PadLeft (parameter) <string>. PadRight (parameter) parameter is the length of the string after adding a space
<string>. PadLeft (parameter 1, parameter 2) parameter 1 is the length to which the string is reached, and parameter 2 is the specified character to be added.

6) Usage of indexof ()
IndexOf ()
Finds the position of the first occurrence of a specified character or string in a string, and returns the first index value, such as:

Str1. IndexOf ("word");//Find the index value (position) of "word" in str1 str1. IndexOf ("string");//Find the index value (position) str1 of the first character of the string in str1. IndexOf ("word", start,end);//from str1 start+1 characters, look for end characters and find the position of "word" in the string STR1

[from the first character] Note: Start+end cannot be greater than the length of str1

7) Use of Insert ()

<string>.insert (parameter 1, parameter 2)

Parameter 1 is the location where the substring is inserted, and parameter 2 is the substring to be inserted

8) Compare the size of the string
Compare (STR1,STR2)--compares the size of two string str1,str2, if it is greater than the returned positive number, equals 0, less than the returned negative number

9) Replace the specified string
String.Replace (parameter 1, parameter 2)--replaces the specified character in a string with the specified character

Related Article

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.