How to remove trailing spaces \n but not trailing tabs \t - groovy

I have strings in Groovy in a tsv file. The values are always strings. In the final column, the strings may sometimes have trailing spaces.
I normally remove these spaces with `.trim(), e.g.
stringHere = 'example '
print(stringHere.trim())
The problem I've noticed with .trim() is that it appears to remove trailing tabs as well.
Is there a method that would only remove \n and not \t?

def stringHere = '12345\t '
//remove spaces only
stringHere = stringHere.replaceAll(/ +$/,'')
println "stringHere = `${stringHere}`"
//let's check
assert stringHere.length()==6
assert stringHere.charAt(5)=='\t'

Related

Remove spaces from a string but not new lines in lua

I used string.gsub(str, "%s+") to remove spaces from a string but not remove new lines, example:
str = "string with\nnew line"
string.gsub(str, "%s+")
print(str)
and I'm expecting the output to be like:
stringwith
newline
what pattern should I use to get that result.
It seems you want to match any whitespace matched with %s but exclude a newline char from the pattern.
You can use a reverse %S pattern (that matches any non-whitespace char) in a negated character set, [^...], and add a \n there:
local str = "string with\nnew line"
str = string.gsub(str, "[^%S\n]+", "")
print(str)
See an online Lua demo yielding
stringwith
newline
"%s" matches any whitespace character. if you want to match a space use " ". If you want to define a specific number of spaces either explicitly write them down " " or use string.rep(" ", 5)

Remove newline character from a string?

I have a string that is like so:
"string content here
"
because it is too long to fit on the screen in one line
The string is the name of a file i would like to read, but i always get an error message that the file name wasn't found because it includes the new line character in the string when this obviously isn't in the file name. I cannot rename the file and I have tried the strip function to remove it, but this doesn't work. How can I remove the enter character from my string so I can load my file?
You can use the function strip to remove any trailing whitespace from a string.
>> text = "hello" + newline; %Create test string.
>> disp(text)
hello
>> text_stripped = strip(text);
>> disp(text_stripped)
hello
>>
In the above ">>" has been included to better present the removal of the whitespace in the string.
Consider replacing the newline character with nothing using strrep. Link
As an example:
s = sprintf('abc\ndef') % Create a string s with a newline character in the middle
s = strrep(s, newline, '') % Replace newline with nothing
Alternatively, you could use regular expressions if there are several characters causing you issues.
Alternatively, you could use strip if you know the newline always occurs at the beginning or end.

tcl : lsort -dictionary replace new lines by spaces

I use the following command to sort the content of a string
set local_object [lsort -dictionary $list_object]
this comand will replace new lines by spaces
how to avoid that ?
lsort assumes that its argument is a Tcl list. Any whitespace including newlines can separate elements of that list, but will not be preserved in the output. If you want to format the output list with one element per line you could do:
set local_object [join [lsort -dictionary $list_object] "\n"]
It all depends on how your list is built. Any string can be interpreted as list. All the white spaces are considered as a delimiter if you're treating string as list.
set str "d b a\n c"
set lst [lsort -dictionary [split $str " "]]
foreach word $lst {
puts $word
}
a
b
c
d
Split has preserved newline and used single space as a delimiter.

Cut off additional \n (new line characters) in a string in R

I would like to cut off unneeded additional new line characters in strings using R.
For example, if I have the string:
"This is an example test string. \n \n \n"
I would like it to look like this:
"This is an example test string. \n"
Try
x <- gsub("\\n\\s*", "\n", x)
This searches for any newline followed by whitespace and replaces it with a single newline

How to strip whitespace in string in TCL?

I need to strip leading and trailing whitespace from a string in TCL. How?
Try this -
      string trim string ?chars?
Returns a value equal to string except that any leading or trailing characters from the set given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).
Original Source :- http://wiki.tcl.tk/10174
try this. this will remove all the withe spaces
[string map {" " ""} $a];
a is your string

Resources