最新消息:关注【已取消】微信公众号,可以获取全套资料,【全套Java基础27天】【JavaEE就业视频4个月】【Android就业视频4个月】

C#常用的字符串操作方法|替换,删除,拆分字符串

扩展 太平洋学习网 0浏览 评论

C#常用的字符串操作方法有替换,删除,拆分字符串等,下面将会介绍这些C#字符串的操作方法,都是很常用的。

1. Replace(C#替换字符串):
public string Replace(char oldChar,char newChar); 在对象中寻找oldChar,如果寻找到,就用newChar将oldChar替换掉。
如:

string st = "abcdef";
string newstring = st.Replace('a', 'x');
Console.WriteLine(newstring);   //即:xbcdef

public string Replace(string oldString,string newString); 在对象中寻找oldString,如果寻找到,就用newString将oldString替换掉。
如:

string st = "abcdef";
string newstring = st.Replace("abc", "xyz");
Console.WriteLine(newstring);   //即:xyzdef

2. Remove(C#删除字符):
public string Remove(int startIndex); 从startIndex位置开始,删除此位置后所有的字符(包括当前位置所指定的字符)。
如:  

string st = "abcdef";
string newstring = st.Remove(4);
Console.WriteLine(newstring);  //即:abcd

public string Remove(int startIndex,int count); 从startIndex位置开始,删除count个字符。
如:  

string st = "abcdef";
string newstring = st.Remove(4,1);
Console.WriteLine(newstring);  //即:abcdf

3. Substring(C#字符串截取):
public string Substring(int startIndex); 从startIndex位置开始,提取此位置后所有的字符(包括当前位置所指定的字符)。
如:  

string st = "abcdef";
string newstring = st.Substring(2);
Console.WriteLine(newstring);  //即:cdef

public string Substring(int startIndex,int count); 从startIndex位置开始,提取count个字符。
如:  

string st = "abcdef";
string newstring = st.Substring(2,2);
Console.WriteLine(newstring);  //即:cd

4. Trim(C#清空空格):
public string Trim ():将字符串对象包含的字符串两边的空格去掉后返回。
public string Trim ( params char[] trimChars ): 从此实例的开始和末尾移除数组中指定的一组字符的所有匹配项。
如:

string st ="abcdef";
//寻找st字符串中开始与末尾是否有与'a'匹配,如有,将其移除。
string newstring = st.Trim(new char[] {'a'});
Console.WriteLine(newstring); //即:bcdef

注:如果字符串为"aaaabcdef",返回依然为bcdef。当移除第一个a时,开始依然为a,继续移除,直到没有。
public string TrimEnd ( params char[] trimChars ): 对此实例末尾与指定字符进行匹配,true则移除
public string TrimStart ( params char[] trimChars ): 对此实例开始与指定字符进行匹配,true则移除

5. ToLower(C#转换大小写)

public string ToLower(): 将字符串对象包含的字符串中的大写全部转换为小写。

6. IndexOf(C#获取指定的字符串的开始索引)
public int IndexOf (sring field):在此实例中寻找field,如果寻找到,返回开始索引,反之,返回-1。
如:

string st = "abcdef";
int num=st.IndexOf("bcd");
Console.WriteLine(num);  //即:1


7. Equals(C#判断字符串是否相等)
public bool Equals (string value): 比较调用方法的字符串对象包含字符串和参数给出的对象是否相同,如相同,就返回true,反之,返回false。
如:

string a = "abcdef";
bool b = a.Equals("bcdef");
Console.WriteLine(b);//即:false

public bool Equals ( string value, StringComparison comparisonType ):比较调用方法的字符串对象包含字符串和参数给出的对象是否在不区分大小写的情况下相同,如相同,就返回true,反之,返回false,第二个参数将指定区域性、大小写以及比较所用的排序规则.
如:

string a = "ABCDEF";
bool b = a.Equals("abcdef",StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine(b);//即:true


8. Split(C#拆分字符串)
public string[] Split ( params char[] separator ):根据separator 指定的没有字符分隔此实例中子字符串成为Unicode字符数组, separator可以是不包含分隔符的空数组或空引用。
public string[] Split ( char[] separator, int count ):参数count 指定要返回的子字符串的最大数量。 
如:

string st = "语文|数学|英语|物理";
string[] split = st.Split(new char[]{'|'},2);
for (int i = 0; i < split.Length; i++)
{
	Console.WriteLine(split[i]);
}

注:count不填则全部拆分

public enum StringSplitOptions 
成员名称                       说明
None                            返回值包括含有空字符串的数组元素
RemoveEmptyEntries   返回值不包括含有空字符串的数组元素

如:

string st = "语文|数学||英语|物理";
string[] split = st.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < split.Length; i++)
{
	Console.WriteLine(split[i]);
}

将StringSplitOptions枚举和Split()方法联系起来:
1.  public string[] Split ( char[] separator, StringSplitOptions options ):options指定StringSplitOptions枚举的RemoveEmptyEntries以省略返回的数组中的空数组元素,或指定StringSplitOptions枚举的None以包含返回的数组中的空数组元
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(C#判断字符串是否存在)
public bool Contains(string text):如果字符串中出现text,则返回true,反之false,如果text为("")也返回true。
如:

string st="语文数学英语";
bool b=st.Contains("语文");
Console.WriteLine(b);//true


10. EndsWith,StartsWith(C#判断字符串的开始或结束)
public bool EndsWith ( string value ): 判断对象包含字符串是否以value指定的字符串结束,是则为 true;否则为 false。 
public bool EndsWith ( string value, StringComparison comparisonType ): 第二个参数设置比较时区域、大小写和排序规则。
public bool StartsWith ( string value ): 判断对象包含字符串是否以value指定的字符串开始,是则为 true;否则为 false。 
public bool StartsWith ( string value, StringComparison comparisonType ) : 第二个参数设置比较时区域、大小写和排序规则。
如:

 string st="语文数学英语abc";
 bool b=st.EndsWith("英语ABC",StringComparison.CurrentCultureIgnoreCase);//第二个参数忽略大小比较。
 Console.WriteLine(b);//true


11. Insert(C#字符串插入)
public string Insert ( int startIndex, string value ):在指定的字符串下标为startIndex前插入字符串value。返回插入后的值。
如:

string st="语文数学英语abc";
string newst=st.Insert(6,"物理");//注:在指定索引“前”插入。
Console.WriteLine(newst);//即:语文数学英语物理abc
来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/kuozhan/547.html
"文章很值,打赏犒劳作者一下"
微信号: Javaweb_engineer

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

与本文相关的文章

发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论