Manipulation of strings in C #

Source: Internet
Author: User

1.Replace (Replacement character):
Public string Replace (char Oldchar,char newchar); Find OldChar in the object and replace OldChar with Newchar if found.
such as:
String st = "abcdef";
string newstring = St. Replace (' A ', ' X ');
Console.WriteLine (newstring); namely: Xbcdef

Public string Replace (string oldstring,string newstring), searching for oldstring in the object, replacing oldstring with newstring if found.
such as:
String st = "abcdef";
string newstring = St. Replace ("abc", "XYZ");
Console.WriteLine (newstring); namely: Xyzdef


2.Remove (delete character):
The public string remove (int startIndex), starting at the StartIndex position, deletes all characters after this position (including the character specified by the current position).
such as:
String st = "abcdef";
string newstring = St. Remove (4);
Console.WriteLine (newstring); namely: ABCD

the public string, remove (int startindex,int count), removes count characters from the StartIndex position.
such as:
String st = "abcdef";
string newstring = St. Remove (4,1);
Console.WriteLine (newstring); namely: ABCDF


3.Substring (String extraction):
Public string Substring (int startIndex), starting at the StartIndex position, extracts all the characters from this position, including the character specified by the current position.
such as:
String st = "abcdef";
string newstring = St. Substring (2);
Console.WriteLine (newstring); namely: Cdef

Public string Substring (int startindex,int count), which extracts count characters starting from the StartIndex position.
such as:
String st = "abcdef";
string newstring = St. Substring (2,2);
Console.WriteLine (newstring); namely: CD

4.Trim (Clear empty lattice):
Public string Trim (): Returns a space after a string object contains a string that is removed from each side.
Public string Trim (params char[] trimChars): Removes all occurrences of a set of characters specified in the array from the beginning and end of this instance.
such as:
String st = "abcdef";
string newstring = St. Trim (new char[] {' A '});//Look for the start and end of the St string if there is a match with ' a ', if so, remove it.
Console.WriteLine (newstring);//i.e.: Bcdef
Note: If the string is "Aaaabcdef", the return is still bcdef. When the first a is removed, the start is still a, which continues to be removed until it is not.
Public string TrimEnd (params char[] trimChars): matches the specified character at the end of this instance, True removes
Public string TrimStart (params char[] trimChars): the instance begins to match the specified character, and true removes

5.ToLower (conversion case)

Public string ToLower (): Converts all uppercase characters in the string contained in the string object to lowercase.

6.IndexOf (Gets the start index of the specified string)
public int IndexOf (sring field): Finds field in this instance, returns the start index if found, and returns-1 instead.
such as:
String st = "abcdef";
int num=st. IndexOf ("BCD");
Console.WriteLine (num); namely: 1

7.Equals (whether equal)
Public bool Equals (string value): The string object that compares the calling method contains the string and the arguments given by the object are the same as the same, returns True, and vice versa, returns false.
such as: string a = "abcdef";
bool B = a.equals ("Bcdef");
Console.WriteLine (b);//That is: false

Public bool Equals (string value, StringComparison ComparisonType): The string object that compares the calling method contains the string and the arguments given by the object are the same in case-insensitive cases, such as the same, Returns True, instead, returns false, and the second parameter specifies the culture, casing, and collation used for the comparison.
such as:
string a = "ABCDEF";
bool B = a.equals ("abcdef", stringcomparison.currentcultureignorecase);
Console.WriteLine (b);//That is: true


8.Split (split)
Public string[] Split (params char[] separator): Separates this instance by the absence of a character specified by separator as a Unicode character array, Separator can be an empty array or a null reference that does not contain delimiters.
Public string[] Split (char[] separator, int count): The parameter count specifies the maximum number of substrings to return.
such as:
String st = "language | mathematics | english | physics";
string[] split = St. Split (New char[]{' | '},2);
for (int i = 0; i < split. Length; i++)
            {
Console.WriteLine (Split[i]);
            }
Note: Count is not filled out and all split

Public enum StringSplitOptions
member name Description
None The return value includes an array element that contains an empty string
removeemptyentries return value does not include an array element that contains an empty string

such as:
String st = "Chinese | math | | English | physics ";
string[] split = St. Split (New char[]{' | '},stringsplitoptions.removeemptyentries);
for (int i = 0; i < split. Length; i++)
            {
Console.WriteLine (Split[i]);
            }
Associate The StringSplitOptions enumeration with the Split () method:
1. Public string[] Split (char[] separator, stringsplitoptions options) : options Specifies the removeemptyentries of the StringSplitOptions enumeration to omit an empty array element from the returned array, or to specify the none of the StringSplitOptions enumeration to contain the empty array elements in the returned array
2. Public string[] Split (char[] separator, int count, stringsplitoptions options)
3. Public string[] Split (string[] separator, stringsplitoptions options)
4. Public string[] Split (string[] separator, int count, stringsplitoptions options)


9.Contains (judging if it exists)
Public bool Contains (string text): Returns True if text appears in the string, or False if text is ("") also returns true.
such as:
string st= "Chinese mathematics English";
bool B=st. Contains ("language");
Console.WriteLine (b);//true


10.endswith,startswith (judging the start or end of a string)
Public bool EndsWith (string value): Determines whether the object contains a string ending with the string specified by value, or true; otherwise false.
Public bool EndsWith (string value, StringComparison ComparisonType): The second parameter sets the time zone, casing, and collation of the comparison.
Public bool StartsWith (string value): Determines whether the object contains a string starting with the string specified by value, or true, otherwise false.
Public bool StartsWith (string value, StringComparison ComparisonType): The second parameter sets the time zone, casing, and collation of the comparison.
such as:
string st= "Chinese mathematics english abc";
bool B=st. EndsWith ("English abc", stringcomparison.currentcultureignorecase);//The second parameter ignores the size comparison.
Console.WriteLine (b);//true

11.Insert (string insertion)
Public string Insert (int startIndex, string value): Inserts the string value before the specified string is labeled StartIndex. Returns the value after insertion.
such as:
string st= "Chinese mathematics english abc";
string newst=st. Insert (6, "physical");//NOTE: Insert before the specified index.
Console.WriteLine (NEWST);//That is: Chinese mathematics english Physics ABC
 
also good.
Original http://www.ffxun.com/content.aspx?id=48&domain=2

Manipulation of strings in C #

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.