Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

c#

helpers

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.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#?

like image 609
Andy Avatar asked Oct 13 '22 19:10

Andy


1 Answers

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).

like image 70
Jason Down Avatar answered Nov 15 '22 08:11

Jason Down