C#中字符串常用的方法

本文介绍了C#中常用的字符串操作方法,包括Replace替换字符、Remove删除字符、Substring提取子字符串、Trim清除空格、ToLower转换大小写、IndexOf查找索引、Equals比较相等、Split切割成数组、Contains判断存在、EndsWith和StartsWith检查开头和结尾,以及Insert插入字符串等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string   str="Hello World."

Replace(替换字符): 把句号替换成感叹号

str=str.RePlace(".","!");	

2.Remove(删除字符):**

//Remove(int startIndex)从startIndex位置开始,删除此位置后所有的字符(包括当前位置所指定的字符)。
 
  string newstring = str.Remove(5);
  Console.WriteLine(newstring); //即:Hello

//Remove(int startIndex,int count)从startIndex位置开始,删除count个字符。
 
      string newstring = st.Remove(6,1);
      Console.WriteLine(newstring); 

3.Substring(字符串提取):**

//Substring(int startIndex);从startIndex位置开始,提取此位置后所有的字符(包括当前位置所指定的字符)。

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

//Substring(int startIndex,int count);从star
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值