替换字符串中的单词 - Ruby [英] Replace words in a string - Ruby

查看:28
本文介绍了替换字符串中的单词 - Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ruby 中有一个字符串:

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 or a loop?

推荐答案

你可以试试用这个方法:

You can try using this way :

sentence ["Robert"] = "Roger"

那么句子就会变成:

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

这篇关于替换字符串中的单词 - Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆