C# 常用截取字符串

来源:互联网 发布:yoast seo 汉化 编辑:程序博客网 时间:2024/05/08 06:06

string str="123abc456";
int i=3;
1 取字符串的前i个字符
   str=str.Substring(0,i); // or  str=str.Remove(i,str.Length-i); 
2 去掉字符串的前i个字符:
   str=str.Remove(0,i);  // or str=str.Substring(i); 
3 从右边开始取i个字符:
  str=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i);
4 从右边开始去掉i个字符:
   str=str.Substring(0,str.Length-i); // or str=str.Remove(str.Length-i,i);
5 如果字符串中有"abc"则替换成"ABC"
   str=str.Replace("abc","ABC");