5

共有する必要がある文字列操作の便利なヘルパーは何ですか?

私はかつて String.Format() の代わりを書いたことがあります。

public static class StringHelpers
{
    public static string Args(this string str, object arg0)
    {
        return String.Format(str, arg0);
    }

    public static string Args(this string str, object arg0, object arg1)
    {
        return String.Format(str, arg0, arg1);
    }

    public static string Args(this string str, object arg0, object arg1, object arg2)
    {
        return String.Format(str, arg0, arg1, arg2);
    }

    public static string Args(this string str, params object[] args)
    {
        return String.Format(str, args);
    }
}

例:

// instead of String.Format("Hello {0}", name) use:
"Hello {0}".Args(name)

C# の文字列には、他にどのような便利なヘルパーがありますか?

4

1 に答える 1

4

より便利な拡張メソッドであるかなり一般的なものは次のとおりです。

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string s)
    {
        return String.IsNullOrEmpty(s);
    }
}

素晴らしいことは何もありませんが、書くことmyString.IsNullOrEmpty()はより便利ですString.IsNullOrEmpty(myString)

于 2010-12-12T08:52:13.513 に答える