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

本文关键字:最后一个 单引号 字符串 修剪 | 更新日期: 2023-09-27 18:37:01

我有一个字符串,末尾有一个新行。我无法选择删除此换行符。它已经在字符串中了。我想删除此字符串中的最后一个单引号。我尝试使用另一篇文章中给出的方法 - 从字符串中修剪最后一个字符

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

尝试执行"Hello! world!".TrimEnd(''');时出现错误

我该如何解决这个问题?

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

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