简体   繁体   中英

Remove space without using string method trim

How can I remove all the left space without removing between & the right space of the string? And also when I changed the value of str the result will be the same. Only the left space will be removed.

function trimLeftSpace() {
    var str = "   Angry Bird   ";
    var splitTrim = str.split('');
    var trimStr = "";
    for (var index = 0; index < splitTrim.length; index++) { //trim left space
        if(splitTrim[index] != " ") {
            trimStr += str[index];
        }
    }
    return trimStr;
 }

To trim the beginning of the string, use a simple regex to replace the whitespaces in the beginning of the string:

 var str = " Angry Bird "; function trimLeftSpace(str) { return str.replace(/^\\s+/, ''); } console.log('"' + trimLeftSpace(str) + '"'); 

Or just use .trimStart() :

 var str = " Angry Bird "; function trimLeftSpace(str) { return str.trimStart(); } console.log('"' + trimLeftSpace(str) + '"'); 

Your current solution creates a new string which contains all the non-space characters of the original string, you need to stop looking for spaces as soon as you find a non-space character. Here is an example:

 function trimLeftSpace(str) { var doneTrimming = false var ret = "" for (var index = 0; index < str.length; index++) { if(str[index] !== ' '){ doneTrimming = true } if(doneTrimming){ ret += str[index] } } return ret; } var result = trimLeftSpace(" Angry Bird "); console.log("|"+result+"|"); 

You could try a regex replacement:

 var str = " Angry Bird "; str = str.replace( new RegExp("^\\\\s+", "gm"),""); console.log('"' + str + '"'); 

This will remove all whitespace on the left of the string:

 function trimLeftSpace(str) { var result = ""; for(var i = 0; i < str.length; i++) { if(str[i] != " ") { return str.slice(i); break; } else { result += str[i]; } } return result; } console.log(trimLeftSpace(" Angry Birds Angry Birds")); 

Try this

function trimLeftSpace(str) {
    return str.replace(/\s+$/, '')
}

var result = trimLeftSpace("   Angry Bird   ");
console.log("|"+result+"|");

If you want to use a function instead of regex solutions from the other answers, then make a function that looks for the first non-space character, then use slice to cut only the part of the string that's after it:

 function customTrim(str) { for(var i = 0; i < str.length; i++) { if(str.charAt(i) !== " ") { return str.slice(i); } } } var res = customTrim(" Snake Shot "); console.log('"' + res + '"'); 

Notes:

  • This only looks for spaces ' ' . If you want to look for tabs '\\t' , newlines '\\n' , ... then just add them to the if test (sperate them with && ).
  • If an empty or space-only strings are passed, then undefined is returned, if you don't want that then just return an empty string at the bottom of the function to make it the default return value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM