无法修剪字符串中的最后一个单引号?

时间:2013-10-28 00:52:55

标签: c#

我的字符串末尾有一个新行。我无法选择删除此换行符。它已经存在于字符串中。我想删除此字符串中的最后一个单引号。我尝试使用另一篇文章[{3}}

中给出的方法
"Hello! world!".TrimEnd('!');

我尝试"Hello! world!".TrimEnd(''');

时出错

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

要修剪string末尾的新行和最后一个引号,请尝试使用.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');

在删除可能的新行后,要从字符串中仅删除最后一个单引号,请执行以下操作:

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);
}