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"
复制代码

持续整理比较不错的?

# 高校智慧校园解决方案摘要 智慧校园解决方案是针对高校信息化建设的核心工程,旨在通过物联网技术实现数字化校园的智能化升级。该方案通过融合计算机技术、网络通信技术、数据库技术和IC卡识别技术,初步实现了校园一卡通系统,进而通过人脸识别技术实现了更精准的校园安全管理、生活管理、教务管理和资源管理。 方案包括多个管理系统:智慧校园管理平台、一卡通卡务管理系统、一卡通人脸库管理平台、智能人脸识别消费管理系统、疫情防控管理系统、人脸识别无感识别管理系统、会议签到管理系统、人脸识别通道管理系统和图书馆对接管理系统。这些系统共同构成了智慧校园的信息化基础,通过统一数据库和操作平台,实现了数据共享和信息一致性。 智能人脸识别消费管理系统通过人脸识别终端,在无需接触的情况下快速完成消费支付过程,提升了校园服务效率。疫情防控管理系统利用热成像测温技术、视频智能分析等手段,实现了对校园人员体温监测和疫情信息实时上报,提高了校园公共卫生事件的预防和控制能力。 会议签到管理系统和人脸识别通道管理系统均基于人脸识别技术,实现了会议的快速签到和图书馆等场所的高效通行管理。与图书馆对接管理系统实现了一卡通系统与图书馆管理系统的无缝集成,提升了图书借阅的便捷性。 总体而言,该智慧校园解决方案通过集成的信息化管理系统,提升了校园管理的智能化水平,优化了校园生活体验,增强了校园安全,并提高了教学和科研的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值