[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to substitute tabs into single spaces for a String

Peter Loftus

4/18/2008 1:33:00 PM

val = " hello ".sub(/\t/, ' ')
puts val

Cant covert tabbed spaces into regular single spaces
Anyone have any ideas?
--
Posted via http://www.ruby-....

5 Answers

Kyle Hunter

4/18/2008 2:01:00 PM

0

Peter Loftus wrote:
> val = " hello ".sub(/\t/, ' ')
> puts val
>
> Cant covert tabbed spaces into regular single spaces
> Anyone have any ideas?

irb(main):006:0> "\tlol\t".gsub("\t", ' ')
=> " lol "
--
Posted via http://www.ruby-....

Phillip Gawlowski

4/18/2008 2:19:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter Loftus wrote:
| val = " hello ".sub(/\t/, ' ')
| puts val
|
| Cant covert tabbed spaces into regular single spaces
| Anyone have any ideas?

You could use a regex that converts whitespace into a single space, in
case tbas aren't the \t character, but are expanded to spaces.

http://doc.infosnel.nl/ruby_regular_expres...

(Best thing I found but Programming Ruby and The Ruby Way cover this,
and Programming Ruby's 1st Edition is available for free.)

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

~ - You know you've been hacking too long when...
...you discover that you're balancing your checkbook in octal.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgIrcEACgkQbtAgaoJTgL/e5gCfdyBs9dyQZ7kIv+linqY74Wuo
6CMAn0apfDaUFdk8SFswZTk9KMuWMWPV
=Whnf
-----END PGP SIGNATURE-----

fedzor

4/18/2008 2:35:00 PM

0


On Apr 18, 2008, at 9:32 AM, Peter Loftus wrote:
> val = " hello ".sub(/\t/, ' ')
> puts val
>
> Cant covert tabbed spaces into regular single spaces
> Anyone have any ideas?

Use gsub instead of sub!

sub only changes the first occurence, gsub chances all.

val = " AWESOME ".gsub(/\t/, ' ')
puts val

--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.



Chris Shea

4/18/2008 4:41:00 PM

0

On Apr 18, 7:32 am, Peter Loftus <lof...@gmail.com> wrote:
> val = " hello ".sub(/\t/, ' ')
> puts val
>
> Cant covert tabbed spaces into regular single spaces
> Anyone have any ideas?
> --
> Posted viahttp://www.ruby-....

The code you posted will only substitute the first tab character into
a single space. If you want all occurrences, use String#gsub.
Secondly, if by "tabbed spaces" you mean that the string doesn't
actually contain tabs, then asking to replace the tab character (\t)
isn't going to do anything.

If, for example, you know tabs have been converted to 4 spaces, you
could do something like this val.gsub(/ {4}/, ' ').

If what you really want is just to have any whitespace converted to a
single space, you could do this: val.gsub(/\s+/, ' ')

HTH,
Chris

Tim Pease

4/18/2008 5:01:00 PM

0


On Apr 18, 2008, at 7:32 AM, Peter Loftus wrote:

> val =3D " hello ".sub(/\t/, ' ')
> puts val
>
> Cant covert tabbed spaces into regular single spaces
> Anyone have any ideas?

Use the String#tr comand

str =3D "string\twith\ttabs"
str.tr("\t", " ")


Blessings,
TwP

(and from the RDoc ...)

str.tr(from_str, to_str) =3D> new_str

Returns a copy of str with the characters in from_str replaced by the =20=

corresponding characters in to_str. If to_str is shorter than =20
from_str, it is padded with its last character. Both strings may use =20
the c1=97c2 notation to denote ranges of characters, and from_str may =20=

start with a ^, which denotes all characters except those listed.

"hello".tr('aeiou', '*') #=3D> "h*ll*"
"hello".tr('^aeiou', '*') #=3D> "*e**o"
"hello".tr('el', 'ip') #=3D> "hippo"
"hello".tr('a-y', 'b-z') #=3D> "ifmmp"