3

C# には、パラメーターを持つTrimEndorTrimStartメソッドがありますか? stringいいえ、そのように使用しても、string.ToCharArray()うまくTrimEnd(str.ToCharArray());動作しません。

例えば:

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

sam等しいはずです"Sam"

4

2 に答える 2

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();
}

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();
    }
于 2012-10-02T12:59:51.530 に答える
-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;
}
于 2012-10-02T12:42:33.767 に答える