Module 1
Strings and StringBuilders
StringBuilders


CC315

StringBuilders

 StringBuilder SB = new StringBuilder();
 StringBuilder SB = new StringBuilder(30);

StringBuilders

StringBuilder SB = new StringBuilder();
SB.append("foo");
SB.append("1234");
int length = SB.length();
String stringSB = SB.toString();

With Strings


    function ENCODER(TEXT,X)
        //TEXT is the text to encode
        //X is the offset
        ENC = ""
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC += CHAR_ENC
            ELSE
                ENC += '*'
        end loop
        return ENC
    end function
 

With StringBuilders


    function SBENCODER(TEXT,X)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

With Strings


    function ENCODER(TEXT,X)
        //TEXT is the text to encode
        //X is the offset
        ENC = ""
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC += CHAR_ENC
            ELSE
                ENC += '*'
        end loop
        return ENC
    end function
 

With StringBuilders


    function SBENCODER(TEXT,X)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

StringBuilder Walk Through


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

StringBuilder Walk Through


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

StringBuilder Walk Through


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

StringBuilder Walk Through


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

StringBuilder Walk Through


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

Fast Forward


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

Fast Forward


    function SBENCODER(TEXT,X)        // call SBENCODER("Data is great!",8)
        //TEXT is the text to encode
        //X is the offset
        ENC = new StringBuilder()
        loop I from 1 to LENGTH of TEXT
            CURRENT = TEXT[I]
            IF CURRENT IS A LETTER
                CHAR_ENC = GET X-th CHAR after CURRENT
                ENC.append(CHAR_ENC)
            ELSE
                ENC.append('*')
        end loop
        return ENC.toString()
    end function
 

Comparison:
String (S) VS StringBuilder(SB)

Iterations Character Copies Memory Addresses
S SB S SB
n (n(n+1))/2 n n + 1 1
1,000,000 500,000,500,000 1,000,000 1,000,001 1