Trim String with for loop - Node.js String

Node.js examples for String:Trim

Description

Trim String with for loop

Demo Code

function trimSpacing(str) {
    var str1 = new String(str);
    var i = 0;/*from w ww  .j a v  a 2  s. c o  m*/
    for (i = 0; i < str1.length; i++) {
        if (str1.charAt(i) != " ") {
            break;
        }
    }
    if (i == str1.length) {
        return "";
    } else {
        var j = i;
        for (i = str1.length - 1; i > -1; i--) {
            if (str1.charAt(i) != " ") {
                break;
            }
        }
        return str1.substring(j, i + 1);
    }
}

Related Tutorials