I have a string of varying length and is usually followed by white spaces (of varying length based on he string).
ie - 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+$/,"");
}
eg
alert("11& 444 DTF# 5676 ".rtrim());
data = data.replace(/\s+$/, "");
\\s
- space +
- one or more 您是否尝试过使用$.trim()
?
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.