Replace words in a string - Ruby

前端 未结 4 1324
星月不相逢
星月不相逢 2020-12-12 11:50

I have a string in Ruby:

sentence = \"My name is Robert\"

How can I replace any one word in this sentence easily without using complex code

4条回答
  •  春和景丽
    2020-12-12 12:01

    You can try using this way :

    sentence ["Robert"] = "Roger"
    

    Then the sentence will become :

    sentence = "My name is Roger" # Robert is replaced with Roger
    

提交回复
热议问题