Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space from a string

Tags:

tcl

Say I have a string like this:

set str "BAT-CAT, DOG,ELEPHANT ,LION & MOUSE-ANT ,MONKEY, DONKEY"

Now I want to get STRING as follows:

"BAT-CAT,DOG,ELEPHANT,LION&MOUSE-ANT,MONKEY,DONKEY"

I am using trim function to remove white space but it is not working kindly suggest any regsub or procedure for same thank you

like image 380
user2901871 Avatar asked Nov 22 '25 11:11

user2901871


2 Answers

The best way to remove all spaces from a string is to use string map:

set stripped [string map {" " ""} $originalString]
like image 177
Donal Fellows Avatar answered Nov 25 '25 09:11

Donal Fellows


regsub -all { } $str ""

This will return: BAT-CAT,DOG,ELEPHANT,LION&MOUSE-ANT,MONKEY,DONKEY

like image 28
Javide Avatar answered Nov 25 '25 10:11

Javide