Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the characters in a string

Tags:

string

ruby

gsub

I have a method that I want to use to replace characters in a string:

def complexity_level_two
  replacements = {
      'i' => 'eye', 'e' => 'eei',
      'a' => 'aya', 'o' => 'oha'}
  word = "Cocoa!55"
  word_arr = word.split('')
  results = []
  word_arr.each { |char|
    if replacements[char] != nil
      results.push(char.to_s.gsub!(replacements[char]))
    else
      results.push(char)
    end
  }
end

My desired output for the string should be: Cohacohaa!55

However when I run this method it will not replace the characters and only outputs the string:

C
o
c
o
a
!
5
5

What am I doing wrong to where this method will not replace the correct characters inside of the string to match that in the hash and how can I fix this to get the desired output?

like image 849
User9123 Avatar asked Dec 23 '16 18:12

User9123


People also ask

How do I replace multiple characters in a string?

Python: Replace multiple characters in a string using the replace() In Python, the String class (Str) provides a method replace(old, new) to replace the sub-strings in a string. It replaces all the occurrences of the old sub-string with the new sub-string.

How do you replace a specific character in a string in Java?

Try using String. replace() or String. replaceAll() instead.


4 Answers

replacements = {
  'i' => 'eye', 'e' => 'eei',
  'a' => 'aya', 'o' => 'oha'}
word = "Cocoa!55"
word.gsub(Regexp.union(replacements.keys), replacements)
#⇒ "Cohacohaaya!55"

Regexp::union, String#gsub with hash.

like image 174
Aleksei Matiushkin Avatar answered Oct 11 '22 19:10

Aleksei Matiushkin


replacements = { 'i' => 'eye', 'e' => 'eei', 'a' => 'aya', 'o' => 'oha' }.
  tap { |h| h.default_proc = ->(h,k) { k } }

"Cocoa!55".gsub(/./, replacements)
  #=> "Cohacohaaya!55"

See Hash#default_proc= and Object#tap.

gsub examines each character of the string. If replacements has that character as a key, the character is replaced with the value of that key in replacements; else (because of the default proc), the character is replaced with itself (that is, left unchanged).

Another way would be to use Hash#fetch:

replacements = { 'i' => 'eye', 'e' => 'eei', 'a' => 'aya', 'o' => 'oha' }

"Cocoa!55".gsub(/./) { |s| replacements.fetch(s) { |c| c } }
  #=> "Cohacohaaya!55"

which, for Ruby v2.2+ (when Object#itself made its debut), can be written

"Cocoa!55".gsub(/./) { |s| replacements.fetch(s, &:itself) }
like image 28
Cary Swoveland Avatar answered Oct 11 '22 17:10

Cary Swoveland


you can try to do this:

my_subs = { 'i' => 'eye', 'e' => 'eei','a' => 'aya', 'o' => 'oha' }
my_word = "Cocoa!55"
my_word.split('').map{|i| my_subs[i] || i}.join

=> "Cohacohaaya!55"

like image 22
IDIR GACI Avatar answered Oct 11 '22 17:10

IDIR GACI


Constructing a method

Define your method with word and subs parameters:

def char_replacer word, subs
  word.chars.map { |c| subs.key?(c) ? subs[c] : c }.join
end

Here we've used the ternary operator which is essentially an if-else expression in a more compact form. Key methods to note are String#chars, Array#map, Hash#key?, see ruby-docs for more info on these. Now with this all set up, you can call your method passing a word string and the subs hash of your choosing.

Example 1

my_subs = { 'i' => 'eye', 'e' => 'eei','a' => 'aya', 'o' => 'oha' }
my_word = "Cocoa!55"

char_replacer my_word, my_subs #=> "Cohacohaaya!55"

Example 2

my_subs = { 'a' => 'p', 'e' => 'c' }
my_word = "Cocoa!55"

char_replacer my_word, my_subs #=> "Cocop!55"
like image 28
Sagar Pandya Avatar answered Oct 11 '22 17:10

Sagar Pandya