How to strip whitespace in string in TCL?
Asked Answered
M

2

7

I need to strip leading and trailing whitespace from a string in TCL. How?

Maletta answered 11/1, 2011 at 12:20 Comment(0)
S
15

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

Statolith answered 11/1, 2011 at 12:27 Comment(2)
There's also string trimleft and string trimright when you only want to trim one end.Scaremonger
Here's an example how the chars argument works: trim (english) vowels from the left and right of a string: set str "each patio"; puts [string trim $str "aeiou"] outputs ch patTimehonored
C
0

try this. this will remove all the withe spaces

[string map {" " ""} $a];

a is your string

Cattier answered 31/12, 2015 at 16:2 Comment(1)
He only wants to strip leading and tailing whitespace. Also you don't remove other types of whitespace, like tabulator, newline...Hobbism

© 2022 - 2024 — McMap. All rights reserved.