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;
}
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;
}
Share
Improve this question
asked Jul 15, 2018 at 10:40
LizQuen UpdateLizQuen Update
612 silver badges8 bronze badges
0
7 Answers
Reset to default 2Your 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+"|");
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) + '"');
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 theif
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.
You could try a regex replacement:
let variable = "hello world";
let removeRegex = /^\s+|\s+$/g;
let removeSpace = variable.replace(removeRegex,"");
console.log(removeSpace);