I'm looking for an example, please, of how to delete one or more spaces from the end of a variable.
(let ((test-variable "hello "))
(if (eq ?\s (aref test-variable (1- (length test-variable))))
(setq test-variable "hello")))
String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.
To remove the whitespace from both the beginning and end- Here, we are applying thetrim() method on string variable myStr, as result, trim method removes the leading and trailing whitespaces and returns the trimmed string after removing whitespaces from myStr string variable.
In JavaScript, trim() is a string method that is used to remove whitespace characters from the start and end of a string.
`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command. Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command.
In Emacs 24.4 (which is to be released later this year) this will be even simpler:
(require 'subr-x)
(string-trim-right "some string ")
While you're waiting for 24.4 to come you can simply define string-trim-right
locally:
(defun string-trim-right (string)
"Remove trailing whitespace from STRING."
(if (string-match "[ \t\n\r]+\\'" string)
(replace-match "" t t string)
string))
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