.trim() in JavaScript not working in IE
Add the following code to add trim functionality to the string. if(typeof String.prototype.trim !== ‘function’) { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ”); } }
Add the following code to add trim functionality to the string. if(typeof String.prototype.trim !== ‘function’) { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ”); } }
EDIT Since c++17, some parts of the standard library were removed. Fortunately, starting with c++11, we have lambdas which are a superior solution. #include <algorithm> #include <cctype> #include <locale> // trim from start (in place) static inline void ltrim(std::string &s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); } // trim from … Read more
If you want to remove leading and ending spaces, use str.strip(): >>> ” hello apple “.strip() ‘hello apple’ If you want to remove all space characters, use str.replace() (NB this only removes the “normal” ASCII space character ‘ ‘ U+0020 but not any other whitespace): >>> ” hello apple “.replace(” “, “”) ‘helloapple’ If you … Read more
For whitespace on both sides, use str.strip: s = ” \t a string example\t ” s = s.strip() For whitespace on the right side, use str.rstrip: s = s.rstrip() For whitespace on the left side, use str.lstrip: s = s.lstrip() You can provide an argument to strip arbitrary characters to any of these functions, like … Read more
A simple answer is: echo ” lol ” | xargs Xargs will do the trimming for you. It’s one command/program, no parameters, returns the trimmed string, easy as that! Note: this doesn’t remove all internal spaces so “foo bar” stays the same; it does NOT become “foobar”. However, multiple spaces will be condensed to single … Read more
To remove all whitespace surrounding a string, use .strip(). Examples: >>> ‘ Hello ‘.strip() ‘Hello’ >>> ‘ Hello’.strip() ‘Hello’ >>> ‘Bob has a cat’.strip() ‘Bob has a cat’ >>> ‘ Hello ‘.strip() # ALL consecutive spaces at both ends removed ‘Hello’ Note that str.strip() removes all whitespace characters, including tabs and newlines. To remove only … Read more
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 … Read more
You can use the substring function: let str = “12345.00”; str = str.substring(0, str.length – 1); console.log(str); This is the accepted answer, but as per the conversations below, the slice syntax is much clearer: let str = “12345.00”; str = str.slice(0, -1); console.log(str);