Removing spaces at both ends of a string in js can be achieved using regular expressions:
String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
Of course the usage is very simple,
var str =" Hi,My Name is Water! "; str.trim();
Of course you can also write a function yourself, or some people are used to this:
function trim(stri) { return stri.replace(/(^\s*)|(\s*$)/g, ""); } var str =" Hi,My Name is Water! "; trim(str);
【Related recommendations 】
1. Share examples of TrimHelper method in Java
2. Detailed explanation of String.trim() method examples in Java