Skip to content

Instantly share code, notes, and snippets.

@aeg
Created May 4, 2013 04:50
Show Gist options
  • Save aeg/5516235 to your computer and use it in GitHub Desktop.
Save aeg/5516235 to your computer and use it in GitHub Desktop.
URI Escapeする。
// Case #1
// URIエスケープする(URLエンコードとかURIエスケープとか言われたりする)
def str = 'http://www.asahi.com/q="test aaa&1344"'
def result = new URLEncoder().encode(str)
assert result == 'http%3A%2F%2Fwww.asahi.com%2Fq%3D%22test+aaa%261344%22'
// Case #2
// 特定のエンコーディング方式を使ってURIエスケープする。
def jstr = 'こんにちは'
// ISO-2022-JP
assert '%1B%24%42%24%33%24%73%24%4B%24%41%24%4F%1B%28%42' == new URLEncoder().encode(jstr, 'ISO-2022-JP')
// Shift-JIS
assert '%82%B1%82%F1%82%C9%82%BF%82%CD' == new URLEncoder().encode(jstr, 'Shift-JIS')
// EUC-JP
assert '%A4%B3%A4%F3%A4%CB%A4%C1%A4%CF' == new URLEncoder().encode(jstr, 'EUC-JP')
// UTF-8
assert '%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF' == new URLEncoder().encode(jstr, 'UTF-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment