trim

JavaScript chop/slice/trim off last character in string

佐手、 提交于 2019-11-26 02:22:54
I have a string, 12345.00 , and I would like it to return 12345.0 . I have looked at trim , but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions? Jon Erickson You can use the substring function: var str = "12345.00"; str = str.substring(0, str.length - 1); // "12345.0" This is the accepted answer, but as per the conversations below, the slice syntax is much clearer: var str = "12345.00"; str = str.slice(0, -1); // "12345.0" You can use slice ! You just have to make sure you know how to use it. Positive #s are relative to the

Replace multiple whitespaces with single whitespace in JavaScript string

只愿长相守 提交于 2019-11-26 02:18:04
问题 I have strings with extra whitespaces, each time there\'s more than only one whitespace I\'d like it be only one. Anyone? I tried searching google, but nothing worked for me. Thanks 回答1: Something like this: var s = " a b c "; console.log( s.replace(/\s+/g, ' ') ) 回答2: You can augment String to implement these behaviors as methods, as in: String.prototype.killWhiteSpace = function() { return this.replace(/\s/g, ''); }; String.prototype.reduceWhiteSpace = function() { return this.replace(/\s+

Remove all whitespace in a string in Python

这一生的挚爱 提交于 2019-11-26 00:12:52
问题 I want to eliminate all the whitespace from a string, on both ends, and in between words. I have this Python code: def my_handle(self): sentence = \' hello apple \' sentence.strip() But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace? 回答1: If you want to remove leading and ending spaces, use str.strip(): sentence = ' hello apple' sentence.strip() >>> 'hello apple' If you want to remove all spaces, use str.replace(): sentence = ' hello apple'

How to trim whitespace from a Bash variable?

僤鯓⒐⒋嵵緔 提交于 2019-11-25 22:47:53
问题 I have a shell script with this code: var=`hg st -R \"$path\"` if [ -n \"$var\" ]; then echo $var fi But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace from $var (like trim() in PHP)? or Is there a standard way of dealing with this issue? I could use sed or AWK, but I\'d like to think there is a more elegant solution to this problem. 回答1: Let's define a variable containing leading, trailing, and

.trim() in JavaScript not working in IE

梦想与她 提交于 2019-11-25 22:38:50
问题 I tried to apply .trim() to a string in one of my JavaScript programs. It\'s working fine under Mozilla, but an error displays when I try it in IE8. Does anyone know what is going on here? Is there anyway I can make it work in IE? code: var ID = document.getElementByID(\'rep_id\').value.trim(); error display: Message: Object doesn\'t support this property or method Line: 604 Char: 2 Code: 0 URI: http://test.localhost/test.js 回答1: Add the following code to add trim functionality to the string.

How do I trim leading/trailing whitespace in a standard way?

别说谁变了你拦得住时间么 提交于 2019-11-25 22:28:36
问题 Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I\'d roll my own, but I would think this is a common problem with an equally common solution. 回答1: If you can modify the string: // Note: This function returns a pointer to a substring of the original string. // If the given string was allocated dynamically, the caller must not overwrite // that pointer with the returned value, since the original pointer must be // deallocated using

What's the best way to trim std::string?

隐身守侯 提交于 2019-11-25 22:21:25
问题 I\'m currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(\" \\n\\r\\t\")+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with elegant alternatives and also left-trim solution are welcome. 回答1: 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>

Trim string in JavaScript?

可紊 提交于 2019-11-25 21:43:03
问题 How do I trim a string in JavaScript? 回答1: All browsers since IE9+ have trim() method for strings. 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 this.replace(rtrim, ''); }; })(); } That said, if using jQuery , $.trim(str) is also available and handles undefined/null. See this: String