Click here to Skip to main content
15,442,716 members
Articles / Programming Languages / C#
Tip/Trick
Posted 23 Nov 2010

Tagged as

Stats

43.5K views
21 bookmarked

StringBuilder Extensions

Rate me:
Please Sign up or sign in to vote.
4.68/5 (27 votes)
23 Nov 2010CPOL
Here are the extensions for StringBuilder that I created to make it work like a string. I have them for years now and finally put in some time to post them here.

Phi Nguyen

public static string Substring(this StringBuilder sb, int startIndex, int length)
{
    return sb.ToString(startIndex, length);
}

public static StringBuilder Remove(this StringBuilder sb, char ch)
{
    for (int i = 0; i < sb.Length; )
    {
        if (sb[i] == ch)
            sb.Remove(i, 1);
        else
            i++;
    }
    return sb;
}

public static StringBuilder RemoveFromEnd(this StringBuilder sb, int num)
{
    return sb.Remove(sb.Length - num, num);
}

public static void Clear(this StringBuilder sb)
{
    sb.Length = 0;
}

/// <summary>
/// Trim left spaces of string
/// </summary>
/// <param name="sb"></param>
/// <returns></returns>
public static StringBuilder LTrim(this StringBuilder sb)
{
    if (sb.Length != 0)
    {
        int length = 0;
        int num2 = sb.Length;
        while ((sb[length] == ' ') && (length < num2))
        {
            length++;
        }
        if (length > 0)
        {
            sb.Remove(0, length);
        }
    }
    return sb;
}

/// <summary>
/// Trim right spaces of string
/// </summary>
/// <param name="sb"></param>
/// <returns></returns>
public static StringBuilder RTrim(this StringBuilder sb)
{
    if (sb.Length != 0)
    {
        int length = sb.Length;
        int num2 = length - 1;
        while ((sb[num2] == ' ') && (num2 > -1))
        {
            num2--;
        }
        if (num2 < (length - 1))
        {
            sb.Remove(num2 + 1, (length - num2) - 1);
        }
    }
    return sb;
}

/// <summary>
/// Trim spaces around string
/// </summary>
/// <param name="sb"></param>
/// <returns></returns>
public static StringBuilder Trim(this StringBuilder sb)
{
    if (sb.Length != 0)
    {
        int length = 0;
        int num2 = sb.Length;
        while ((sb[length] == ' ') && (length < num2))
        {
            length++;
        }
        if (length > 0)
        {
            sb.Remove(0, length);
            num2 = sb.Length;
        }
        length = num2 - 1;
        while ((sb[length] == ' ') && (length > -1))
        {
            length--;
        }
        if (length < (num2 - 1))
        {
            sb.Remove(length + 1, (num2 - length) - 1);
        }
    }
    return sb;
}

/// <summary>
/// Get index of a char
/// </summary>
/// <param name="sb"></param>
/// <param name="c"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, char value)
{
    return IndexOf(sb, value, 0);
}

/// <summary>
/// Get index of a char starting from a given index
/// </summary>
/// <param name="sb"></param>
/// <param name="c"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, char value, int startIndex)
{
    for (int i = startIndex; i < sb.Length; i++)
    {
        if (sb[i] == value)
        {
            return i;
        }
    }
    return -1;
}

/// <summary>
/// Get index of a string
/// </summary>
/// <param name="sb"></param>
/// <param name="text"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value)
{
    return IndexOf(sb, value, 0, false);
}

/// <summary>
/// Get index of a string from a given index
/// </summary>
/// <param name="sb"></param>
/// <param name="text"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value, int startIndex)
{
    return IndexOf(sb, value, startIndex, false);
}

/// <summary>
/// Get index of a string with case option
/// </summary>
/// <param name="sb"></param>
/// <param name="text"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value, bool ignoreCase)
{
    return IndexOf(sb, value, 0, ignoreCase);
}

/// <summary>
/// Get index of a string from a given index with case option
/// </summary>
/// <param name="sb"></param>
/// <param name="text"></param>
/// <param name="startIndex"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value, int startIndex, bool ignoreCase)
{
    int num3;
    int length = value.Length;
    int num2 = (sb.Length - length) + 1;
    if (ignoreCase == false)
    {
        for (int i = startIndex; i < num2; i++)
        {
            if (sb[i] == value[0])
            {
                num3 = 1;
                while ((num3 < length) && (sb[i + num3] == value[num3]))
                {
                    num3++;
                }
                if (num3 == length)
                {
                    return i;
                }
            }
        }
    }
    else
    {
        for (int j = startIndex; j < num2; j++)
        {
            if (char.ToLower(sb[j]) == char.ToLower(value[0]))
            {
                num3 = 1;
                while ((num3 < length) && (char.ToLower(sb[j + num3]) == char.ToLower(value[num3])))
                {
                    num3++;
                }
                if (num3 == length)
                {
                    return j;
                }
            }
        }
    }
    return -1;
}

/// <summary>
/// Determine whether a string starts with a given text
/// </summary>
/// <param name="sb"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool StartsWith(this StringBuilder sb, string value)
{
    return StartsWith(sb, value, 0, false);
}

/// <summary>
/// Determine whether a string starts with a given text (with case option)
/// </summary>
/// <param name="sb"></param>
/// <param name="value"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static bool StartsWith(this StringBuilder sb, string value, bool ignoreCase)
{
    return StartsWith(sb, value, 0, ignoreCase);
}

/// <summary>
/// Determine whether a string is begin with a given text
/// </summary>
/// <param name="sb"></param>
/// <param name="value"></param>
/// <param name="startIndex"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static bool StartsWith(this StringBuilder sb, string value, int startIndex, bool ignoreCase)
{
    int length = value.Length;
    int num2 = startIndex + length;
    if (ignoreCase == false)
    {
        for (int i = startIndex; i < num2; i++)
        {
            if (sb[i] != value[i - startIndex])
            {
                return false;
            }
        }
    }
    else
    {
        for (int j = startIndex; j < num2; j++)
        {
            if (char.ToLower(sb[j]) != char.ToLower(value[j - startIndex]))
            {
                return false;
            }
        }
    }
    return true;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
svjavier13-Sep-12 8:35
Membersvjavier13-Sep-12 8:35 
GeneralMy vote of 3 Pin
cuong-nm4-Sep-12 20:23
Membercuong-nm4-Sep-12 20:23 
GeneralMy vote of 5 Pin
Paul C Smith17-Jul-12 10:27
MemberPaul C Smith17-Jul-12 10:27 
SuggestionWildcardReplace method that's been a useful addition for me... Pin
Paul C Smith17-Jul-12 11:09
MemberPaul C Smith17-Jul-12 11:09 
GeneralReason for my vote of 5 Thanks for sharing. Pin
linuxjr30-Nov-10 8:24
professionallinuxjr30-Nov-10 8:24 
GeneralThanks for sharing. You might want to consider some changes... Pin
grgran29-Nov-10 12:14
Membergrgran29-Nov-10 12:14 
GeneralReason for my vote of 5 Have you also considered posting you... Pin
Tatworth29-Nov-10 8:11
MemberTatworth29-Nov-10 8:11 
GeneralReason for my vote of 5 Great one man, thanks :-) Pin
thatraja27-Nov-10 23:12
professionalthatraja27-Nov-10 23:12 
GeneralReason for my vote of 2 - Pin
parthenium25-Nov-10 4:41
Memberparthenium25-Nov-10 4:41 
GeneralHere's a couple more from my own project (MBG Extensions Lib... Pin
vnmatt24-Nov-10 1:41
Membervnmatt24-Nov-10 1:41 
GeneralThich nhieu lam! Cam on! Have a five! ;-) Pin
vnmatt24-Nov-10 1:39
Membervnmatt24-Nov-10 1:39 
GeneralYou might want to look at the String.Trim, String.TrimStart,... Pin
Andrew Rissing23-Nov-10 9:11
MemberAndrew Rissing23-Nov-10 9:11 
GeneralReason for my vote of 5 Thanks for sharing :) Pin
Eddy Vluggen23-Nov-10 8:11
professionalEddy Vluggen23-Nov-10 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.