I want to add a whitespace before and after random strings.
I have tried using "Random_string".center(1, " ") but it doesnt work.
Thanks
I want to add a whitespace before and after random strings.
I have tried using "Random_string".center(1, " ") but it doesnt work.
Thanks
My ruby is rusty, but IMO nothing wrong with the easy way
def pad( random )
" " + random + " "
end
padded_random_string = pad("random_string")
using center
"random_string".center( "random_string".length + 2 )
irb(main):001:0> x='Random String'
=> "Random String"
irb(main):002:0> y=' '+x+' '
=> " Random String "
irb(main):003:0> x.center(x.length+2)
=> " Random String "
The parameter to center
is the total length of the desired output string (including padding).
I mean, is there some reason that you can't just do this?
padded_string = ' ' + random_string + ' '