从字符串中删除最后一个字符

195

我有一个字符串,比如说

"Hello! world!" 

我想要对字符串进行修剪或删除操作,将字符串中的感叹号从"world!"中移除,但是不要从"Hello!"中移除。

1
也许超出了您的要求,但我可以问一下,您能否花点时间考虑使用我提出的正则表达式呢? - Marcello Faga
1
可能是删除字符串的最后一个字符的重复问题。 - Tor Klingberg
2
在C#8+中,您可以使用“范围”来执行此操作:"Hello! world!"[..^1]; // 产生 "Hello! world" - Granger
15个回答

2
你也可以使用这个:
public static class Extensions
 {

        public static string RemovePrefix(this string o, string prefix)
        {
            if (prefix == null) return o;
            return !o.StartsWith(prefix) ? o : o.Remove(0, prefix.Length);
        }

        public static string RemoveSuffix(this string o, string suffix)
        {
            if(suffix == null) return o;
            return !o.EndsWith(suffix) ? o : o.Remove(o.Length - suffix.Length, suffix.Length);
        }

    }

2
一个简化此过程的示例扩展类:-
internal static class String
{
    public static string TrimEndsCharacter(this string target, char character) => target?.TrimLeadingCharacter(character).TrimTrailingCharacter(character);
    public static string TrimLeadingCharacter(this string target, char character) => Match(target?.Substring(0, 1), character) ? target.Remove(0,1) : target;
    public static string TrimTrailingCharacter(this string target, char character) => Match(target?.Substring(target.Length - 1, 1), character) ? target.Substring(0, target.Length - 1) : target;

    private static bool Match(string value, char character) => !string.IsNullOrEmpty(value) && value[0] == character;
}

Usage

"!Something!".TrimLeadingCharacter('X'); // Result '!Something!' (No Change)
"!Something!".TrimTrailingCharacter('S'); // Result '!Something!' (No Change)
"!Something!".TrimEndsCharacter('g'); // Result '!Something!' (No Change)

"!Something!".TrimLeadingCharacter('!'); // Result 'Something!' (1st Character removed)
"!Something!".TrimTrailingCharacter('!'); // Result '!Something' (Last Character removed)
"!Something!".TrimEndsCharacter('!'); // Result 'Something'  (End Characters removed)

"!!Something!!".TrimLeadingCharacter('!'); // Result '!Something!!' (Only 1st instance removed)
"!!Something!!".TrimTrailingCharacter('!'); // Result '!!Something!' (Only Last instance removed)
"!!Something!!".TrimEndsCharacter('!'); // Result '!Something!'  (Only End instances removed)

1

这是@Damian Leszczyński - Vash的稍作修改版本,它将确保只有特定字符被删除。

public static class StringExtensions
{
    public static string TrimLastCharacter(this string str, char character)
    {
        if (string.IsNullOrEmpty(str) || str[str.Length - 1] != character)
        {
            return str;
        }
        return str.Substring(0, str.Length - 1);
    }
}

0

我选择使用TrimEnd编写扩展程序,只是因为我已经在内联中使用它并且感到满意...

static class Extensions
{
        public static string RemoveLastChars(this String text, string suffix)
        {            
            char[] trailingChars = suffix.ToCharArray();

            if (suffix == null) return text;
            return text.TrimEnd(trailingChars);
        }

}

确保在使用静态类时,将命名空间包含在您的类中;P。用法如下:

string _ManagedLocationsOLAP = string.Empty;
_ManagedLocationsOLAP = _validManagedLocationIDs.RemoveLastChars(",");          

-5
如果你想从特定表达式中(在这里是"world")移除'!'字符,那么你可以使用这个正则表达式。
string input = "Hello! world!";

string output = Regex.Replace(input, "(world)!", "$1", RegexOptions.Multiline | RegexOptions.Singleline);

// result: "Hello! world"

$1特殊字符包含所有匹配的“world”表达式,并用于替换原始的“world!”表达式


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接