sevk 2009-04-03 12:36
浏览 234
已采纳

RUBY把帮助替换成中文字怎么处理?

这个是python的,如果是RUBY,如何转化这种字符:帮 ,有没有unichr什么的?:
[code="python"]

!/usr/bin/env python

-*- coding: utf-8 -*-

import re

str='帮助'
def unistr(m):
return unichr(int(m.group(0)[2:-1]))

print re.sub(r'&#[0-9]{5};',unistr,str)

[/code]

  • 写回答

2条回答 默认 最新

  • Hooopo 2009-04-03 18:54
    关注

    有个库叫HTML Entities for Ruby(http://htmlentities.rubyforge.org/)
    1.[code="java"]gem install htmlentities[/code]
    2.
    [code="java"]require 'htmlentities'
    coder = HTMLEntities.new
    string = '帮助'
    result=coder.decode(string) [/code]
    3.windows下可能由于结果是utf8编码会结果会显示乱码
    [code="java"]require'iconv'
    def conv(str)
    Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",str)
    end
    puts conv(result)[/code]

    [code="java"]=>帮助[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?