当前位置:网站首页>String operation methods: replace, delete and split strings

String operation methods: replace, delete and split strings

2022-06-11 02:27:00 Xia 78

1、C# Replace string ):

public string Replace(char oldChar,char newChar); Look for... In objects oldChar, If you find , Just use newChar take oldChar Replace .

Such as :

string st = “abcdef”;

string newstring = st.Replace(‘a’, ‘x’);

Console.WriteLine(newstring); // namely :xbcdef

2、Remove(C# Delete string ):

public string Remove(int startIndex); from startIndex Position start , Delete all characters after this position ( Include the characters specified in the current position ).

Such as :

string st = “abcdef”;

string newstring = st.Remove(4);

Console.WriteLine(newstring); // namely :abcd

3、Substring(C# String interception ):

public string Substring(int startIndex); from startIndex Position start , Extract all characters after this position ( Include the characters specified in 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); from startIndex Position start , extract count Characters .
Such as :

string st = “abcdef”;

string newstring = st.Substring(2,2);

Console.WriteLine(newstring); // namely :cd

4、Split(C# Split string )

public string[] Split ( params char[] separator ): according to separator The specified substring without character separation for this instance becomes Unicode A character array , separator It can be an empty array without delimiters or an empty reference .

public string[] Split ( char[] separator, int count ): Parameters count Specifies the maximum number of substrings to return .

Such as :

string st = “ Chinese language and literature | mathematics | English | Physics ”;

string[] split = st.Split(new char[]{’|’},2);

for (int i = 0; i < split.Length; i++)

{

Console.WriteLine(split[i]);

}

原网站

版权声明
本文为[Xia 78]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020614161744.html