Trim string in JavaScript
All browsers since IE9+ have trim()
method for strings:
" \n test \n ".trim(); // returns "test" here
For those browsers who does not support trim()
, you can use this polyfill from MDN:
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
That said, if using jQuery
, $.trim(str)
is also available and handles undefined/null.
See this:
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim=function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim=function(){return this.replace(/\s+$/,'');};
String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};
Trim specific character from a string
One line is enough:
var x = '|f|oo||';
var y = x.replace(/^\|+|\|+$/g, '');
document.write(x + '<br />' + y);
How can i trim this javascript string?
Is it always a pound symbol? Just replace it.
val = val.replace('£', ''); // then trim
Javascript - Apply trim function to each string in an array
String.prototype.trim.apply
is the Function.prototype.apply
method without being bound to trim
. map
will invoke it with the string, the index and the array as arguments and nothing (undefined
) for the this
Arg - however, apply
expects to be called on functions:
var apply = String.prototype.trim.apply;
apply.call(undefined, x[0], 0, x) // TypeError
What you can do is passing the trim
function as the context for call
:
[' aa ', ' bb '].map(Function.prototype.call, String.prototype.trim)
// ['aa', 'bb']
What happens here is
var call = Function.prototype.call,
trim = String.prototype.trim;
call.call(trim, x[0], 0, x) ≡
trim.call(x[0], 0, x) ≡
x[0].trim(0, x); // the arguments don't matter to trim
trim string from first number in the string in javascript
you can try this,
var title = "IX - Delivery Received 1/15/2018 9:34:00 AM ";
var digit = title.search(/\d/);
var finalname = title.substring(0, digit);
Which outputs "IX - Delivery Received "
How to trim an unwanted continuous string at end in javascript?
Working off Deryck's comments & answer, but allowing for any number of dashes on the end:
const longStr = "Hello ---- TecAdmin------------";
var thing = longStr.replace(/-*$/g, '');
console.log(thing);
Remove all white space from string JavaScript
Use this:
str.replace(/\s+/g, '');
Instead of this:
str.trim()
How can I trim a specific character off the start & end of a string?
You can use RegEx to easily conquer this problem:
myString = myString.replace(/^"+|"+$/g, '');
You can substitute the "
with any character (be careful, some characters need to be escaped).
Here's a demo on JSFiddle.
An explanation of the regular expression:
/
- start RegEx (/
)
^"+
- match the start of the line (^
) followed by a quote ("
) 1 or more times (+
)
|
- or
"+$
- match a quote ("
) 1 or more times (+
) followed by the end of the line ($
)
/
- end RegEx (/
)
g
- "global" match, i.e. replace all
Remove space without using string method trim
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+"|");
Related Topics
Usage of the Backtick Character (') in JavaScript
JavaScript "New Array(N)" and "Array.Prototype.Map" Weirdness
Why Do Regex Constructors Need to Be Double Escaped
How to Make an Ajax Call Without Jquery
How to Sort an Array of Integers Correctly
How to Use Multiple Versions of Jquery on the Same Page
What Does the Exclamation Mark Do Before the Function
Accessing the Web Page'S Http Headers in JavaScript
How to Append Something to an Array
Origin Is Not Allowed by Access-Control-Allow-Origin
How to Insert an Item into an Array At a Specific Index (JavaScript)
How to Get the Current Date in JavaScript
How to Loop Through a Plain JavaScript Object With the Objects as Members
What's the Prettiest Way to Compare One Value Against Multiple Values
Refresh Image With a New One At the Same Url
Get the Name of an Object'S Type
Is It Right to Think of a JavaScript Function Expression That Uses the 'New' Keyword as 'Static'