首页 > ruby > Ruby字符处理的几个实例

Ruby字符处理的几个实例

2009年5月5日 发表评论 阅读评论

Ruby的字符处理能力是比较强大的,以下是我利用ruby处理汉字字符时摸索出的几个实例:

删除:
char = “查看位置 某路某小区”
char = char.delete(‘查看位置’)
其中char来源于网页,有的时候会显示乱码,可能是网页和文本编辑器的编码不一样造成的。
这时可以使用:
char = char[8 .. 996]

截取:
char = “3室2厅1厨1卫2阳台”
想截取“3室”中的“3”、“2厅”中的“2”和“1卫”中的“1”
char_1 = char[0,1]
char_4 = char[3,1]
char_10 = char[9,1]

匹配:
if (char_1 =~  /3/) && (char_4 =~  /2/) && (char_10 =~  /1/)
ie152.radio(:id, /3室2厅1卫/).set
end

本文原创于 http://www.sdgz.cn,转摘请注明出处。



  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
      
                  
      

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word