带字符串参数的C#TrimStart

Mik*_*ole 61 c#

我正在寻找String扩展方法,TrimStart()TrimEnd()接受一个字符串参数.

我自己可以建立一个,但我总是对看别人如何做事感兴趣.

如何才能做到这一点?

Pat*_*ald 87

要修剪(完全匹配)字符串的所有匹配项,您可以使用以下内容:

TrimStart

public static string TrimStart(this string target, string trimString)
{
    if (string.IsNullOrEmpty(trimString)) return target;

    string result = target;
    while (result.StartsWith(trimString))
    {
        result = result.Substring(trimString.Length);
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

TrimEnd

public static string TrimEnd(this string target, string trimString)
{
    if (string.IsNullOrEmpty(trimString)) return target;

    string result = target;
    while (result.EndsWith(trimString))
    {
        result = result.Substring(0, result.Length - trimString.Length);
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

要从目标的开始/结束修剪trimChars中的任何字符(例如,"foobar'@"@';".TrimEnd(";@'")将返回"foobar"),您可以使用以下内容:

TrimStart

public static string TrimStart(this string target, string trimChars)
{
    return target.TrimStart(trimChars.ToCharArray());
}
Run Code Online (Sandbox Code Playgroud)

TrimEnd

public static string TrimEnd(this string target, string trimChars)
{
    return target.TrimEnd(trimChars.ToCharArray());
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个好的解决方案,因为它还修剪了部分匹配:"foobartes".TrimEnd("test".ToCharArray())导致foobar.如果你得到一个完全匹配,它应该只修剪.编辑:quickfix:在修剪前添加一个target.EndsWith(trimChars)检查 (15认同)
  • 老实说,关于`TrimStart/End(trimChars.ToCharArray());`的部分应该被删除.除了在一个天真的测试中然后在生产使用中使用字符串时,这几乎不会像预期的那样工作. (4认同)
  • 感谢`string.ToCharArray()`提示. (2认同)

Run*_*tad 16

TrimStart和TrimEnd接收一组字符.这意味着您可以将字符串作为char数组传递,如下所示:

var trimChars = " .+-";
var trimmed = myString.TrimStart(trimChars.ToCharArray());
Run Code Online (Sandbox Code Playgroud)

所以我认为不需要带有字符串参数的重载.

  • 请注意,此解决方案与**任何**顺序中的修剪字符匹配.例如,如果`myString ="Sammy"`和`trimChars ="Sam"`它将返回"y",但预期的结果是"my".因此它修剪得太多,并且不会将修剪词视为这样. (17认同)
  • 正如@Matt 所解释的,这种方式有点冒险。发生这种情况是因为 Trim(Start/End) 采用字符数组作为参数,而不是字符串。鉴于该示例,您还可以使用 `myString="Sammy"` 和 `trimChars="SaX"`;这将导致“mmy”。确保这是您想要的行为 (2认同)

Tim*_*Tim 12

我认为问题是试图从一个较大的字符串的开头修剪一个特定的字符串.

例如,如果我有字符串"hellohellogoodbyehello",如果你试图调用TrimStart("你好"),你会得到"goodbyehello".

如果是这种情况,您可以使用如下代码:

string TrimStart(string source, string toTrim)
{
    string s = source;
    while (s.StartsWith(toTrim))
    {
        s = s.Substring(toTrim.Length - 1);
    }
    return s;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要进行大量的字符串修剪,这将不会超级高效,但如果仅仅针对少数情况,那么它很简单并且可以完成工作.

  • 这应该是`toTrim.length`,没有`-1`对吧? (3认同)

Fab*_*doy 6

要匹配整个字符串而不分配多个子字符串,您应该使用以下命令:

    public static string TrimStart(this string source, string value, StringComparison comparisonType)
    {
        if (source == null)
        {
            throw new ArgumentNullException(nameof(source));
        }

        int valueLength = value.Length;
        int startIndex = 0;
        while (source.IndexOf(value, startIndex, comparisonType) == startIndex)
        {
            startIndex += valueLength;
        }

        return source.Substring(startIndex);
    }

    public static string TrimEnd(this string source, string value, StringComparison comparisonType)
    {
        if (source == null)
        {
            throw new ArgumentNullException(nameof(source));
        }

        int sourceLength = source.Length;
        int valueLength = value.Length;
        int count = sourceLength;
        while (source.LastIndexOf(value, count, comparisonType) == count - valueLength)
        {
            count -= valueLength;
        }

        return source.Substring(0, count);
    }
Run Code Online (Sandbox Code Playgroud)