【问题标题】:How do I remove the last character from a string only when it is a comma?仅当它是逗号时,如何从字符串中删除最后一个字符?
【发布时间】:2019-11-15 00:43:42
【问题描述】:

我有:

String test = "Hello,";
String test2 = "Hello";

如果最后一个字符是空字符串的逗号,我想替换它。我目前有:

String var = test.Remove(str.Length - 1, 1) + "";
String var = test2.Remove(str.Length - 1, 1) + "";

这总是会替换最后一个字符,即使它不是逗号。

【问题讨论】:

标签: c# string replace text-manipulation


【解决方案1】:

你可以像这样使用.TrimEnd:

String test = "Hello,";
test = test.TrimEnd(',');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多