The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

This is just a code dump of some useful string extension methods that I make use of daily. Use it, dont use it...:)

UPDATE 23 JUL 2008: I've included the functions in the code that were submitted by Nik Smit (with some editing). Also, here's a link to a file with everthing in a class.

 Expand Code
public static class StringExtensions
{
 
    public static string Left(this string str, int length)
    {
        return (str.Substring(0, length < str.Length ? length : str.Length));
    }
 
    public static string Right(this string str, int length)
    {
        return (str.Substring(length < str.Length ? str.Length - length : 0));
    }
 
    public static string Reverse(this string str)
    {
        char[] c = str.ToCharArray();
        Array.Reverse(c);
        return new string(c);
    }
 
    public static string ToSentenceCase(this string str)
    {
        string returnValue = str.Left(1).ToUpper();
        if (str.Length > 1)
            returnValue += str.Substring(1).ToLower();
        return (returnValue);
    }
 
    public static string ToProperCase(this string str)
    {
        var returnValue = new StringBuilder();
        string[] values = str.Split(' ');
        foreach (string s in values)
            returnValue.Append(s.ToSentenceCase() + " ");
        return (returnValue.ToString().Left(str.Length));
    }
 
    public static bool IsAlphaNumeric(this string inputString)
    {
        if (inputString == null) return false;
        return Regex.Matches(inputString, "[^A-Za-z0-9]$").Count == 0;
    }
 
    public static string StripNonAlphaNumeric(this string inputString, string replacement)
    {
        if (inputString == null) return null;
        return Regex.Replace(inputString, "[^A-Za-z0-9]", replacement, RegexOptions.None);
    }
 
    public static string StripNonNumeric(this string inputString, string replacement)
    {
        if (inputString == null) return null;
        return Regex.Replace(inputString, "[^\\.\\-0-9]", replacement, RegexOptions.None);
    }
 
    public static string TruncateAndTrim(this string str, int length)
    {
        return (str.Trim().Left(length));
    }
}

posted on Monday, July 21, 2008 3:30 PM

Feedback

# re: Some String extension methods 7/22/2008 9:51 AM Nik Smit
Ah - good old vb6 function names :)

Not sure how this will come out in the comment, but here are some I use fairly often.

public static bool IsAlphaNumeric(this string inputString)
{
if (inputString == null) return false;
return Regex.Matches(inputString, "[^A-Za-z0-9]$").Count == 0;
}

public static string StripNonAlphaNumeric(this string inputString, string replacement)
{
if (inputString == null) return null;
return Regex.Replace(inputString, "[^A-Za-z0-9]", replacement, RegexOptions.None);
}

public static string StripNonNumeric(this string inputString, string replacement)
{
if (inputString == null) return null;
return Regex.Replace(inputString, "[^\\.\\-0-9]", replacement, RegexOptions.None);
}

public static string Truncate(this string stringToShorten, int maxLength)
{
if (stringToShorten.Length < maxLength)
{
return stringToShorten;
}
else
{
return stringToShorten.Substring(0, maxLength);
}
}



public static string TruncateAndTrim(this string stringToShorten, int maxLength)
{
if (stringToShorten == null) return null;
stringToShorten = stringToShorten.Trim();
if (stringToShorten.Length < maxLength)
{
return stringToShorten;
}
else
{
return stringToShorten.Substring(0, maxLength);
}
}




public static string Ellipsis(this string stringToShorten, int length)
{
if (stringToShorten == null) return null;
if (stringToShorten.Length > length) return stringToShorten.Substring(0, length) + "...";
return stringToShorten;
}

public static bool IsValidUri(this string url)
{
if (url.IsNullOrEmpty()) return false;
try
{
Uri uri = new Uri(url);
return true;
}
catch
{
return false;
}
}



# re: Some String extension methods 7/23/2008 1:18 PM Darksider
Very nice - I'll add them to my example, if you don't mind.

Comments have been closed on this topic.