Replace words in a string - Ruby

前端 未结 4 1322
星月不相逢
星月不相逢 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:18

    First, you don't declare the type in Ruby, so you don't need the first string.

    To replace a word in string, you do: sentence.gsub(/match/, "replacement").

提交回复
热议问题