考虑到字符串:

"Hello there world"

如何创建URL编码的字符串,如下所示:

"Hello%20there%20world"

我也想知道如果字符串也有其他符号,比如:

"hello there: world, how are you"

最简单的方法是什么?我要分析并构建一些代码.

推荐答案

2019年,乌里.encode已过时,不应使用.


require 'uri'

URI.encode("Hello there world")
#=> "Hello%20there%20world"
URI.encode("hello there: world, how are you")
#=> "hello%20there:%20world,%20how%20are%20you"

URI.decode("Hello%20there%20world")
#=> "Hello there world"

Ruby-on-rails相关问答推荐

Rails turbo流只更新页面上的一个元素

错误AESGCMOpen获取密码:消息身份验证失败:Golang解密GCM

Rails:比枚举更惯用的排序/可比较列表方法?

序列化来自关联的不可变数据是个好主意吗?

向现有控制器添加操作(Ruby on Rails)

rails 3.2 迁移无法在更改方法中向 create_table 添加索引

fields_for 表单构建器对象为零

Rspec/Capybara 正在加载,循环要求被认为是有害的

Rails 5:无法从参数中检索哈希值

Rails 引擎中的迁移?

如果缺少翻译,则回退到默认语言

在 factory_girl 中填充与 children 的关联

rails structure.sql和schema.rb有什么区别

Rails - 具有空数组的强参数

我如何在具有活动记录的 Rails 中使用两个不同的数据库?

在 rails 3 中批量插入

counter_cache 实现的问题

Dotenv 多行变量

如何在 Rails 迁移中判断数据库类型?

何时(如果)合并 ActiveRecord 迁移?