gsub blank

P

Pen Ttt

irb(main):011:0> " gk".gsub("\s","h")
=> "hhhhhgk"
irb(main):012:0> " gk".gsub("\s+","h")
=> " gk"
how can i make " gk" into "hgk"?
 
H

Hassan Schroeder

irb(main):011:0> " =A0 =A0 gk".gsub("\s","h")
=3D> "hhhhhgk"
irb(main):012:0> " =A0 =A0 gk".gsub("\s+","h")
=3D> " =A0 =A0 gk"
how can i =A0make =A0" =A0 =A0 gk" into =A0"hgk"?
=3D> "hgk"

--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

irb(main):011:0> " gk".gsub("\s","h")
=> "hhhhhgk"
irb(main):012:0> " gk".gsub("\s+","h")
=> " gk"
how can i make " gk" into "hgk"?
Your second example is almost there, except that it is a String, not a
Regexp. Regexp are enclosed with slashes rather than quotes (Regexp has it's
own literal, as opposed to languages like Java, where it needs to be defined
within a String)

" gk".gsub( /\s+/ , "h") # => "hhghkh"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

regex problem with gsub 3
how to convert the string? 10
urls from gsub not correct 1
gsub UTF values 0
using a \ in gsub 4
gsub("\\", "\\\\") seems unintuitive 10
Partial GSUB match / replacement 6
Socket hang in thread 1

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top