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

Ste*_*eam 0 c#

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

"Hello! world!".TrimEnd('!');
Run Code Online (Sandbox Code Playgroud)

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

我该如何解决 ?

Dan*_*mms 5

要从a的末尾修剪新行和最后一个引用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');
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)