75

次の文字列を指定します。

"Hello there world"

次のような URL エンコードされた文字列を作成するにはどうすればよいですか。

"Hello%20there%20world"

文字列に次のような他の記号も含まれている場合の対処方法も知りたいです。

"hello there: world, how are you"

そうする最も簡単な方法は何ですか?解析してから、そのためのコードを作成するつもりでした。

4

4 に答える 4

134

2019 年、URI.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"
于 2013-06-29T02:12:52.647 に答える