C# 中接受字符串的 TrimEnd 或 TrimStart 方法?

Mny*_*kka 3 c# string

C# 是否有带有参数的TrimEndorTrimStart方法string?不,即使我们string.ToCharArray()像这样使用TrimEnd(str.ToCharArray());它也表现不佳。

例如:

string sam = "Sammy";
sam = TrimEndOrStart(sam, "my", true);
Run Code Online (Sandbox Code Playgroud)

sam应该等于"Sam"

obe*_*eak 5

您可以使用 StartWith / EndWith ,例如:

static class StringTrimExtension {
    public static string TrimStart(this string value, string toTrim) {
        if (value.StartsWith(toTrim)) {
            int startIndex = toTrim.Length;
            return value.Substring(startIndex);
        }
        return value;
    }

    public static string TrimEnd(this string value, string toTrim) {
        if (value.EndsWith(toTrim)) {
            int startIndex = toTrim.Length;
            return value.Substring(0, value.Length - startIndex);
        }
        return value;
    }
}

static void Main(string[] args) {
        string s = "Sammy";
        Console.WriteLine(s);
        string trimEnd = s.TrimEnd("my");
        string trimStart = s.TrimStart("Sa");
        Console.WriteLine(trimEnd);
        Console.WriteLine(trimStart);
        Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

如果您需要ignoreCase,您可以实现并覆盖您的trim 方法,并使用StartsWith / EndsWith方法中的覆盖。

编辑: 通过递归:

static class StringTrimExtension {
    public static string TrimStartRecursive(this string value, string toTrim) {
        string result = value;
        while (TrimStart(result, toTrim, out result)) {}
        return result;
    }

    public static string TrimEndRecursive(this string value, string toTrim) {
        string result = value;
        while (TrimEnd(result, toTrim, out result)) { }
        return result;
    }

    public static string TrimStart(this string value, string toTrim) {
        string result;
        TrimStart(value, toTrim, out result);
        return result;
    }

    public static string TrimEnd(this string value, string toTrim) {
        string result;
        TrimEnd(value, toTrim, out result);
        return result;
    }

    public static bool TrimStart(this string value, string toTrim, out string result) {
        result = value;
        if (value.StartsWith(toTrim)) {
            int startIndex = toTrim.Length;
            result= value.Substring(startIndex);
            return true;
        }
        return false;
    }

    public static bool TrimEnd(this string value, string toTrim, out string result) {
        result = value;
        if (value.EndsWith(toTrim)) {
            int startIndex = toTrim.Length;
            result = value.Substring(0, value.Length - startIndex);
            return true;
        }
        return false;
    }
}

    static void Main(string[] args) {
        string s = "Sammymymymy";
        Console.WriteLine(s);
        string trimEnd = s.TrimEndRecursive("my");
        string trimStart = s.TrimStartRecursive("Sa");
        Console.WriteLine(trimEnd);
        Console.WriteLine(trimStart);
        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)