基礎知識
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"
複製程式碼
持續整理比較不錯的?