Reputation: 93146
I want to add a whitespace before and after random strings.
I have tried using "Random_string".center(1, " ") but it doesnt work.
Thanks
Upvotes: 5
Views: 14904
Reputation: 11
"Random_string".ljust("Random_string".length + 4).rjust("Random_string".length + 8)
or
"Random_string".ljust(17).rjust(21) # where "Random_string" is 13 characters long
using .ljust method with .rjust method
Upvotes: 1
Reputation: 16431
I find this to be the most elegant solution:
padded_string = " #{random_string} "
Nothing wrong with taking the easy way.
Upvotes: 6
Reputation: 10748
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 )
Upvotes: 3
Reputation: 124632
I mean, is there some reason that you can't just do this?
padded_string = ' ' + random_string + ' '
Upvotes: 4