JavaScript doesn't seem to have a native trim()
method. How can I trim white spaces at the start and end of a string with JavaScript?
trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.
The trim_left() function removes all the leading white spaces. The trim_right() function removes all the trailing white spaces. The trim() function removes all the leading and trailing white spaces.
The shortest form for jQuery:
string = $.trim(string);
Link
according to this page the best all-around approach is
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
Of course if you are using jQuery , it will provide you with an optimized trim method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With