C#几个经常用到的字符串截取


一、字符串截取问题:

//1. = /current year/#####, ##### = same no. from

Case Profile No:CAP-0001003-2022-00143   Case No.:0001003/2022/00143

entity.crms_case_no = entity.crms_case_profile_no.Remove(0, 4).Replace("-", "/");

二、扩展常用字符串

 1. 取字符串的前i个字符

 string str1=str.Substring(0,i);

string str1=str.Remove(i,str.Length-i);

2. 去掉字符串的前i个字符

string str1=str.Remove(0,i);

string str1=str.SubString(i);

3.从右边开始取i个字符:

string str1=str.SubString(str.Length-i);

string str1=str.Remove(0,str.Length-i);

4.从右边开始去掉i个字符:

string str1=str.Substring(0,str.Length-i);

string str1=str.Remove(str.Length-i,i);

5.如果字符串中有"abc"则替换成"ABC"

str=str.Replace("abc","ABC");

6.c#截取字符串最后一个字符

str1.Substring(str1.LastIndexOf(",")+1);

k = k.Substring(k.Length-1, 1);

三、参考资料

备注:20220209