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

时间:2012-10-02 12:42:34

标签: c# string

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

例如:

string sam = "Sammy";
sam = TrimEndOrStart(sam, "my", true);

sam应该等于"Sam"

2 个答案:

答案 0 :(得分:4)

您可以使用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();
}

如果您需要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();
    }

答案 1 :(得分:-3)

/// <summary>
/// Does a nice trim for your string
/// </summary>
/// <param name="Subject">The subject</param>
/// <param name="Search">What to trim on the string</param>
/// <param name="TrimEnd">If set to true we trim the end</param>
/// <returns></returns>
public static string TrimEndOrStart(string Subject, string Search, bool TrimEnd)
{
    if (TrimEnd)
    {
        if (Subject.EndsWith(Search) == false)
        {
            return Subject;
        }

        Subject = ReverseString(Subject);

        Start:
            string[] Array1 = Subject.Split(new string[] { ReverseString(Search) }, 2, StringSplitOptions.RemoveEmptyEntries);

        if (Array1.Length == 1)
        {
            if (Subject.Equals(Array1[0], StringComparison.Ordinal) == false)
            {
                Subject = Array1[0];
                goto Start;
            }
        }

        Subject = StaticMethods.ReverseString(Subject);
        return Subject;
    }

    if (Subject.StartsWith(Search) == false)
    {
        return Subject;
    }

    Start2:
        string[] Array2 = Subject.Split(new string[] { Search }, 2,  StringSplitOptions.RemoveEmptyEntries);

    if (Subject.Equals(Array2[0], StringComparison.Ordinal) == false)
    {
        Subject = Array2[0];
        goto Start2;
    }

    return Subject;
}


/// <summary>
/// Reverses a string array
/// </summary>
/// <param name="StrArray"></param>
/// <returns></returns>
public static string ReverseString(string Str)
{
    char[] ObjectArr = Str.ToCharArray();
    char[] NewObjectArr = new char[ObjectArr.Length];
    int counter = 0;

    for (int index = ObjectArr.Length - 1; index > -1; index--)
    {
        NewObjectArr[counter] = ObjectArr[index];
        counter = counter + 1;
    }

    Str = "";
    StringBuilder StrBu = new StringBuilder(1000);

    for (int index = 0; index < NewObjectArr.Length; index++)
    {
        StrBu.Append(NewObjectArr[index]);
    }

    Str = StrBu.ToString();
    return Str;
}