Add a null character to terminate string
let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + ' ' + lastPart ; firstPart = str . substr ( 0 , len - 1 ) ; lastPart = str . substr ( len ) ; str = firstPart + ' ' + lastPart ; str [ len - 1 ] = ' ' ; return str ; }