C#删除字符串最后一个字符的几种方法(Several methods of c# deleting the last character of a string)

案例:删除字符串中的最后一个字符

string s = "8,2,3,7,9,"  //处理后变成 “8,2,3,7,9”

方法一:使用Substring

string s = "8,2,3,7,9,"
s = s.Substring(0,s.Length - 1)

方法二、用TrimEnd,这个方法传递的是一个字符数组

string s = "8,2,3,7,9,"
s=s.TrimEnd(',')
//如果要删除"9,",则需要这么写
char[] MyChar = {'9',','};
s = s.TrimEnd(MyChar);

方法三、用Remove

string s = "8,2,3,7,9,"
s = s.Remove(a.Length - 1,1); //移除掉","
————————

Case: delete the last character in the string

string s = "8,2,3,7,9,"  //处理后变成 “8,2,3,7,9”

方法一:使用Substring

string s = "8,2,3,7,9,"
s = s.Substring(0,s.Length - 1)

Method 2. Use trimend. This method passes a character array

string s = "8,2,3,7,9,"
s=s.TrimEnd(',')
//如果要删除"9,",则需要这么写
char[] MyChar = {'9',','};
s = s.TrimEnd(MyChar);

方法三、用Remove

string s = "8,2,3,7,9,"
s = s.Remove(a.Length - 1,1); //移除掉","