Depending on your version of JavaScript (i.e., regular expression support), you may also be able to do:
function TrimString(str) { str = str.replace(/^[ ]+(.*)$/, '$1'); // Trims leading spaces str = str.replace(/^(.*)[ ]+$/, '$1'); // Trims trailing spaces return str; }