StringBuilder: To Build or Not to Build StringBuilder class is a common class that many have become accustom to, including me.

An individual posted a question regarding if there was a comparable method in typescript. Several solutions were mention or linked, but none contained a class. Here is a small class that embodies some of the common StringBuilder methods. 

In addition, when joining numeric strings, special care must be taken with the normal typescript/javascript operators. The StringBuilder class will treat all as strings and not attempt a numeric conversion and add values.

It can be expanded upon greatly to fit your needs. 



export class StringBuilder
 {
    strArray: Array&lt;string&gt; = new Array&lt;string&gt;();
    constructor()
    {
        
    }
    Get(nIndex:number): string
    {
      let str:string = null;
      if( (this.strArray.length &gt; nIndex) &amp;&amp; (nIndex &gt;= 0) )
      {
         str = this.strArray[nIndex];
      }
      return(str);
    }
    IsEmpty(): boolean
    {
       if(this.strArray.length == 0) return true;
       return(false);
    }
    Append(str: string): void
    {
        if(str != null)
           this.strArray.push(str);
    }
    ToString(): string
    {
        let str:string = this.strArray.join("");
        return(str);
    }

    ToArrayString(delimeter: string): string
    {
        let str:string = this.strArray.join(delimeter);
        return(str);
    }

    Clear()
    {
        this.strArray.length = 0;
    }
 }
In JavaScript/TypeScript it is custom to place the opening braces at the end of the line instead of on a new line by itself.

Insert a space between control statements and the opening round brackets. (Also don't put round brackets around the expression of a return statement.)

Always use braces even when you only have one line after the statement.

Example: 

if (str != null) {
   this.strArray.push(str);
}


Always have a space between the colon and type.

Method names should start with a lower case letter, especially since you should be overriding toString.

There should be a blank line between methods.



The empty constructor is pointless and should be left out.

The Get method is not really a good idea. The fact that your string builder uses an array internally shouldn't influence the interface of the class.

If you do have a Get method like this, then you should consider emulating the signature of a regular array and return undefined instead of null, when the index is out of bounds. That actually would save you the checking of the bounds yourself:

get(nIndex: number): string {
    return this.strArray[nIndex];
}


In IsEmpty the expression this.strArray.length == 0 (should be a === BTW) returns a boolean, so you can return that directly:

isEmpty(): boolean {
    return this.strArray.length === 0;
}


Also this may not be correct way to determine an empty StringBuilder. If the array contained only empty strings, shouldn't the StringBuilder still be consider empty? But this could be covered by the next point.

Append should not only ignore null as a parameter, but also undefined and empty strings, so just do:

append(str: string): void {
    if (!str) {
        this.strArray.push(str);
    }
}


ToArrayString doesn't seem to be a good method name. I'd suggest something like joinToString. Also there is no need to use a variable in there. 

joinToString(delimeter: string): string {
    return this.strArray.join(delimeter);
}


And avoid a little bit of duplicate code by having toString call ToArrayString/joinToString.

toString(): string {
    return this.joinToString("");
}