This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Help: with removing multiple spaces.

Old posts that have not been replied to for several years.
Locked
s
skirmis

Help: with removing multiple spaces.

Post by skirmis »

Hello,

Can someone help with this, I need to remove multiple spaces from string. For example I have "hello I am bot". I need to get this format "hello I am bot". How can I replace two or more spaces into one ?

Thanks for advices.
SkIRmiS
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The forum, will not display spaces correctly, unless you place them in a "CODE" block.

However, this should remove the spaces.

Code: Select all

proc remspaces {string} {
  set a [list]
  foreach b [split $string] {
    if {$b == ""} { continue }
    lappend a $b
  }
  return [join $b]
}
Use like code

Code: Select all

set mytext "hello  with      multiple spaces"
set mytext [remspaces $mytext]
s
skirmis

Post by skirmis »

Hi,
Something is wrong here, because I got only the last word of string.

-----------------------------------
proc remspaces {string} {
set a
  • foreach b [split $string] {
    if {$b == ""} { continue }
    lappend a $b
    }
    return [join $b]
    }

    set dat ""
    set dat "here is a test"
    set dat [remspaces $dat]

    putnotc $nick $dat

    -----------------------------------

    And result is : test

    Thanks in advice :)
    [/code]
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

My fault.

In that code, change the "return [join $b]" to "return [join $a]"
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

I myself would just use something like:

Code: Select all

regsub -all {  [^ ]*} $text "" text
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

I hate regular expressions.

I fully well understand there power, but I simply can't understand how to create/format them properly.

Most of the reasons why I do things a slightly hard, yet still simple way.
s
skirmis

Post by skirmis »

Thanks to all who helped me :) everything is working fine now !

Bye
SkIRmiS
Locked