Ruby 札記 - 閒理字符串

基礎知識

Ruby 字符串類型爲 String
* 默認使用 Unicode 字符集,可以直接轉碼,操作 UTF-8

?> "Hello".class
=> String
?> '中國'.to_s
=> "中國"
?> '中國'.encoding.to_s
=> "UTF-8"
  • 多行字符串
# 其中 {} 可以換成 <> ## !! 等
?> z = %q{My name is
Grac
Kanil
.}
=> "My name is\nGrac\nKanil\n."
  • 快速定義字符串數組,%w{}使用空白符分隔字符串
?> x = %w{a b c d}
=> ["a", "b", "c", "d"]
>> y = ["a", "b", "c"]
=> ["a", "b", "c"]
>> x == y
=> false
>> y << "d"
=> ["a", "b", "c", "d"]
>>
?> x == y
=> true
>>
?> x.class
=> Array
  • Ruby Doc 文檔模式字符串
x = <<END_GEK
This is the Document
And this is pretty.
END_GEK
  • 字符串連接
"hello" + "ruby"
#=> "helloruby"
  • 字符串複製多次
"abc" * 3
#=> "abcabcabc"
  • 字符串比較,以 ASCII 值比較,
puts "x" > "y"
#=> false

# 獲取字符 ASCII 值,Ruby 版本不同不同
puts ?x
puts "x".ord
#=> 120

# ASCII 值獲取字符
puts 120.chr
#=> "x"
  • 字符串插寫,和其他模板語言類似
>> puts "#{10 + 20}"
30
=> nil
?> puts "#{ "cool" * 2 }"
coolcool
=> nil

字符串處理函數

  • 字符串長度
"GracKanil".length
#=> 9

"GracKanil".size
#=> 9
  • 大小寫
>> "Grac".upcase
=> "GRAC"
>> "Grac".downcase
=> "grac"
  • 逆序和大小寫
>> "Grac".reverse
=> "carG"
>> "Grac".swapcase
=> "gRAC"
>> "Grac".reverse.upcase
=> "CARG"
  • hash
>> "Grac".hash
=> -4302903494964065754
  • 字符是否包含子串
"hello".include?"lo"
#=> true

"hello".include?"loc"
#=> false
  • 字符串插入
"1234".insert(0, "G")
#=> "G1234"
"1234".insert(3, "G")
#=> "123G4"
"1234".insert(-1, "G")
#=> "1234G"
  • 字符串分割
# 默認分割符爲空格
"a b c d".split
#=> ["a", "b", "c", "d"]
?> "Grac".split(//)
=> ["G", "r", "a", "c"]
>> "Grac".split(//, 3)
=> ["G", "r", "ac"]
>> "G,r,a,c".split(",")
=> ["G", "r", "a", "c"]
>> "G,r,a,c,".split(",")
=> ["G", "r", "a", "c"]
>> "G,r,a,c,,".split(",")
=> ["G", "r", "a", "c"]
>> "G,r,a,c,,".split(%r{,\s*})
=> ["G", "r", "a", "c"]
  • 字符串替換
>> "Grac Kanil".gsub(/rac/, "###")
=> "G### Kanil"
>> "Grac Kanil".sub(/.a/, "##")
=> "G##c Kanil"
>> "Grac Kanil".gsub(/.a/, "##")
=> "G##c ##nil"

>> s = "Grac"
=> "Grac"
>> s.replace "Kanil"
=> "Kanil"
>> s
=> "Kanil"
  • 字符串刪除
>> "Grac".delete "r"
=> "Gac"
>> "Grac".delete "rb-d"
=> "Ga"
  • 除去字符串兩端空格
?> s = " Grac "
=> " Grac "
>> s.lstrip
=> "Grac "
>> s.rstrip
=> " Grac"
>> s.strip
=> "Grac"
>> s
=> " Grac "
  • 字符串轉數字
?> "12345".to_i
=> 12345
?> "1234ssa".to_i
=> 1234
  • 匹配子串的位置
?> "Grac Kanil" =~ /an/
=> 6
>> "Grac Kanil" =~ /[a]/
=> 2
>> "Grac Kanil" =~ /[a]+/
=> 2
  • match 方法
>> puts "matched" if "Grac Kanil".match(/[ai]/)
matched
=> nil
  • 獲取字符串方法
String.methods
#=> [:try_convert, :allocate, :new, :superclass, :freeze, 
:===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, 
:constants, :const_get, :const_set, :const_defined?, 
:const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, 
:include, :prepend, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, 
:private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~, :!~, :eql?, :hash, 
:class, :singleton_class, :clone, :dup, :itself, :taint, 
:tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, 
:public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, 
:tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, 
:object_id, :to_enum, :enum_for, :equal?, :!, :!=, 
:instance_eval, :instance_exec, :__send__, :__id__]

舉個栗子

# 處理字符串去重
str = "AAA;BBBB;AAA;CC;AAA;CCC;BBBB"
str.split(";").uniq.join(";")
# "AAA;BBBB;CC;CCC"

持續整理比較不錯的��

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章