开发者

What is the most useful String helper that you've encountered? [closed]

开发者 https://www.devze.com 2023-01-29 22:30 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

What useful helpers for String manipulation to you have to share?

I once wrote a replacement for String.Format(), which I find much more neat to use:

public static class StringHelpers
{
    public static string Args(this string str, object arg0)
    {
        return String.开发者_StackOverflow社区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);
    }
}

Example:

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

What other useful helpers do you have for strings in C#?


A fairly popular one that is more of a convenience extension method is the following:

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

Nothing great, but writing myString.IsNullOrEmpty() is more convenient than String.IsNullOrEmpty(myString).

0

精彩评论

暂无评论...
验证码 换一张
取 消