I have a string of varying length and is usually followed by white spaces (of varying length based on he string).
i.e. - the string is always 20 characters long
var data = "DUR IT R4356 " //with 8 trailing
or the string could be开发者_运维问答
var data = "11& 444 DTF# 5676 " //with 3 trailing
What is the best way to get rid of those trailing white spaces?
I was thinking some JS function that goes to the last character that is not a white space, then replace all the white spaces with empty string ?
Any suggestions? If jQuery is better for this I am open to that as well...
Thanks.
Here are some useful trimming functions you can use:
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+$/,"");
}
e.g.
alert("11& 444 DTF# 5676 ".rtrim());
data = data.replace(/\s+$/, "");
\s
- space+
- one or more
Have you tried using $.trim()
?
精彩评论