Several ways to delete the last character in a string in C #

1. Use TrimEnd, public string TrimEnd (params char [] trimChars); the parameter is a character array

 string str = "abcde";
 str = str.TrimEnd('e');

string s = " from table union all ";
s = s.Trim().TrimEnd("union all".ToCharArray());

result:

2. SubString interception: Substring (int startIndex, int length);

string str = "abcde";
str = str.Substring(0, str.Length-1);

3.Remove:Remove(int startIndex, int count);

string str = "abcde";
str = str.Remove(str.Length - 1, 1);

 

Published 30 original articles · Like1 · Visits1158

Guess you like

Origin blog.csdn.net/chunchunlaila/article/details/105440038