你遇到过的最有用的String助手是什么?

And*_*ndy 5 c# string helpers

您必须分享哪些有用的字符串操作助手?

我曾经写过一个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);
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

// instead of String.Format("Hello {0}", name) use:
"Hello {0}".Args(name)
Run Code Online (Sandbox Code Playgroud)

你对C#中的字符串有什么其他有用的帮助?

Jas*_*own 4

一种相当流行的方法更像是一种方便的扩展方法,如下所示:

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string s)
    {
        return String.IsNullOrEmpty(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

没什么了不起,但是写起来比方便myString.IsNullOrEmpty()多了。String.IsNullOrEmpty(myString)