0

我有一个字符串,末尾有一个新行。我不能选择删除这个换行符。它已经在字符串中了。我想删除此字符串中的最后一个单引号。我尝试使用另一篇文章中给出的方法 - Trim last character from a string

"Hello! world!".TrimEnd('!');

当我尝试做时出现错误"Hello! world!".TrimEnd(''');

我该如何解决 ?

4

1 回答 1

5

To trim the new line(s) and last quote(s) from the end of a string, try using .TrimEnd(params char[])

string badText = "Hello World\r\n'";

// Remove all single quote, new line and carriage return characters
// from the end of badText
string goodText = badText.TrimEnd('\'', '\n', '\r');

To remove only the last single quote from a string after removing the possible new line(s), do something like this:

string badText = "Hello World\r\n'";
string goodText = badText.TrimEnd('\n', '\r');
if (goodText.EndsWith("'"))
{
    // Remove the last character
    goodText = goodText.Substring(0, goodText.Length - 1);
}
于 2013-10-28T00:59:21.130 回答