Interchange characters in Ruby string


Hi friends,

In ruby we can interchange characters in a string easily by using “.tr” methods this is a good useful method to perform some replace actions on strings.

Returns a copy of str with the characters in from_str replaced by the corresponding characters in to_str.

Here are few examples on this method:

This is the Example for Exchange the strings..tr ‘aeiou’, ‘_
=> “Th_s _s th_ Ex_mpl_ f_r Exch_ng_ th_ str_ngs.
‘This is the Example for Exchange the strings.’.tr ‘a-k’, ‘*’
=> “T**s *s t** Ex*mpl* *or Ex***n** t** str*n*s.”

‘This is the Example for Exchange the strings.’.tr ‘a-z’, ‘s’
=> “Tsss ss sss Essssss sss Esssssss sss sssssss.”

‘This is the Example for Exchange the strings.’.tr ‘aeiou’, ‘5’
=> “Th5s 5s th5 Ex5mpl5 f5r Exch5ng5 th5 str5ngs.”

One thought on “Interchange characters in Ruby string

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s