How to strip whitespace in string in TCL? - string

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

Related

TCL: How to remove all letters/numbers from a string?

I am using tcl programming language and trying to remove all the letters or numbers from a string. From this example, I know a general way to remove all the letters from a string (e.x. set s abcdefg0123456) is
set new_s [string trim $s "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXXYZ"]
If I want to remove all numbers from a string in general, I can do
set new_s [string trim $s "0123456789"]
Is there a more straightforward way to remove all letters/numbers?
I also notice if I want to remove a portion of numbers (e.x. 012) instead of all numbers, the following does NOT work.
set new_s [string trim $s "012"]
Can someone explain why?
Use regular expressions:
set s abcdefg0123456
regsub -all {\d+} $s {} new_s ;# Remove all digits
regsub -all {[[:alpha:]]+} $s {} new_s ;# Remove all letters
To answer your other question: string trim (and string trimleft and string trimright as “half” versions) removes a set of characters from the ends of a string (and returns the new string; it's a pure functional operation). It doesn't do anything to the interior of the string. It doesn't know anything about patterns. The default set of characters removed is “whitespace” (spaces, newlines, tabs, etc.)
When you do:
set new_s [string trim $s "012"]
You are setting the removal set to 0, 1 and 2, but it is still only the ends that get removed. Thus it will leave x012101210y entirely alone, but turn 012101210 into the empty string.

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.

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

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'

Unescape a string with escaped sequences in Delphi

I use Delphi 5 and have a String like this from a http-connection:
str :='content=bell=7'#$8'size=20'#$8'other1'#$D#$A#$8'other2'
This string contains some sequence with escape characters and i want to unescape these characters. If I use the trim function, the escape sequence are still inside. Maybe this is because '#$8' is no viewable sign?
How can i replace '#&8' separately. For example with '&', so that i get the string:
str1 :='content=bell=7&size=20&other1'#$D#$A'&other2'
After this I can use trim to unescape the other sequences.
str2 :='content=bell=7&size=20&other1#13#10&other2'
Those are Delphi character sequences. The compiler interprets them as it processes your source file. It converts #$8 into a backspace character in the string. If you want to replace that character with something else, you could call StringReplace. (If that's your real code, then you could just skip the extra function call and use the desired characters in the string literal directly in your code.)
str2 := StringReplace(str1, #8, '&', [rfReplaceAll]);
Trim removes whitespace from the start and end of a string, but your characters aren't at either end.

How do I trim leading and trailing whitespace in Common Lisp?

How do I trim leading and trailing whitespace in Common Lisp?
CL-USER> (string-trim
'(#\Space #\Newline #\Backspace #\Tab
#\Linefeed #\Page #\Return #\Rubout)
" A string ")
"A string"
string-left-trim and string-right-trim for leading and trailing whitespace, respectively.

Resources