how to capitalize a number of characters in a word

C

Cheyne Li

Hi, there

For example, how can i get "HelLoha" from "helloha"

Looking forward to hearing from you. Thanks in advance
 
M

Marc Heiler

For example, how can i get "HelLoha" from "helloha"

Probably one hundred million ways. One could be this:


x = "helloha" # => "helloha"
x.capitalize! # => "Helloha"
x[2,1] = x[2,1].capitalize # => "L"


x # => "HeLloha"


And yeah instead of 2,1 you would do 3,1.
 
T

Todd Benson

Hi, there

For example, how can i get "HelLoha" from "helloha"

Here's one for fun...

SEPARATION, s = 32, 'helloha'
(my_letters = [0, 3]).each {|b| s -= SEPARATION}
puts s

...it's goofy, because I'm golfing and also trying to be descriptive
at the same time :)

Of course, I'm making a large assumption that the letters to be
capitalized are based on position.

Here's a contrived one...

include 'matrix'
(Vector[*'helloha'.unpack('C*')] - Vector[1,0,0,1,0,0,0] * 32).to_a.pack('C*')

...umm, ugly.

Todd
 
R

Raveendran Jazzez

Raveendran said:
Hi Li,

irb(main):016:0> a="HelLoha"
=> "HelLoha"
irb(main):017:0> a.downcase
=> "helloha"
irb(main):018:0>
So simply reversible

irb(main):016:0> a="HelLoha"
=> "HelLoha"
irb(main):017:0> a.downcase
=> "helloha"
irb(main):018:0> a="helloha"
=> "helloha"
irb(main):019:0> a=a.capitalize!
=> "Helloha"
irb(main):020:0> a=a[0,3]+a[3,1].capitalize!+a[4,3]
=> "HelLoha"
irb(main):021:0>
 
T

Todd Benson

Hi Li,

irb(main):016:0> a="HelLoha"
=> "HelLoha"
irb(main):017:0> a.downcase
=> "helloha"
irb(main):018:0>

Regards,
P.Raveendran

There must be a language barrier. I thought the point was to turn
'helloha' _into_ 'HelLoha', and not the reverse.

Todd
 
T

Todd Benson

Yes. mistake from my side sorry for the inconvenience.

Well, you might be right with your first post, since you never know
what the original poster meant. Not everyone, after all, has a
command of the english language. And that would include me, lol.

Todd
 
C

Cheyne Li

Marc said:
For example, how can i get "HelLoha" from "helloha"

Probably one hundred million ways. One could be this:


x = "helloha" # => "helloha"
x.capitalize! # => "Helloha"
x[2,1] = x[2,1].capitalize # => "L"


x # => "HeLloha"


And yeah instead of 2,1 you would do 3,1.

Thank you very much. It helps a lot
 
R

Rubén Medellín

class String
def capitalize_at(*indices)
str = self.dup
indices.each do |i|
if chr = self[i, 1]
str[i,1] = chr.upcase
else
raise IndexError.new("character #{i} out of index")
end
end
str
end
end

"helloha".capitalize_at(0, 3) # => "HelLoha"

If you find yourself doing it frequently.
 
M

Marc Heiler

There must be a language barrier.

Isn't it, after all, the "natural" languages which separate us all
mostly, while Ruby unites our thinking brains ... :)

Cheyne Li, Ruben's example is probably the best if you think you need
to do this more often. Ruby by default has .capitalize already, so I'd
guess a .capitalize_at method seems like a good name.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top