GBString 一個可以識別GB18030編碼的字串ruby類
專案的homepage
http://rubyforge.org/projects/gbstring/
License
====================
GBString, a ruby class simliar to String class but with GB18030 encoding aware style.
Copyright (C) <2007> <Bob Yang>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Email: bob.yang.dev at gmail.com
====================
一、快速開始
====================
1、構造一個GBString物件
require "g_b_string"
gbstr = GBString.new("這是一個GB18030編碼的字串!") # 注意,檔案的編碼方式要是GB18030
或者使用下面的簡單方式
gbstr = _c("這也是一個GB18303編碼的字串!")
2、按中文字元為基本單位計算字串長度
gbstr = _c("中文串")
puts( gbstr.size ) # => 3 而不是 6
3、遍歷中文字串
_c("中文串abc").each do |char|
puts char
end
# => 中
# => 文
# => 串
# => a
# => b
# => c
二、更多用法
====================
可以參考 test/tc_g_b_string.rb
1、each, each_with_index
每個元素是一箇中文字元,型別為String.
2、split
cstr = _c"第一 第二 第三"
tokens = cstr.split(" ")
tokens[0] # => 第一
tokens[1] # => 第二
tokens[3] # => 第三
3、下標操作符'[ ]', 注意得到的物件是GBString型別的
cstr = _c"甲A"
puts( cstr[0] ) # => 甲
puts( cstr[1] ) # => A
puts( cstr[0].class ) # => GBString
puts( cstr[1].class ) # => GBString
4、型別轉換,to_a, range
# 轉換到陣列, to_a
gbstr = _c("型別map")
array = gbstr.to_a
puts array.size # => 5
puts array[0] # => 類
puts array[3] # => a
# 作為range使用, 得到的物件是GBString類的
gbstr = _c"This is a /"中文字串/"!"
puts( gbstr[10..16] ) # => "中文字串"
puts( gbstr[-8..-1] ) # => "中文字串"!
puts( gbstr[10, 7] ) # => "中文字串"
5、作為Hash的key使用, 與相同內容的String物件相等.
hash = {_c("中國")=>1, _c("貴州省")=>2, _c("貴陽市")=>3}
puts( hash[_c("貴陽市")] ) # => 3
puts( hash["貴陽市"] ) # => 3
三、執行單元測試
====================
進入 test 目錄,執行 ruby tc_g_b_string.rb 即可。
如果一切正常,會提示:
7 tests, 56 assertions, 0 failures, 0 errors