Ruby: String

intro

  • strings are a sequences of characters.
  • the name comes from the fact that the characters are strung together.

string format

  • single quoted ('string') : basic string, faster than double quoted.
  • double quoted ("string") : support interpolation and escape sequences.
  • back quoted (`string`): execute as command in bin/rails folder.
  • other.%q quoted (%q(foo bar): string without interpolation.
  • other.%Q quoted (%Q(foo #{bar})): string with interpolation.

fancy way

  • concate: 'foo' << 'bar'
  • multiply: 'foo' * 1000
  • 'aaa'.next #=> 'aab'
  • '111'.next #=> '112'

search methods

  • check whether a string contains any given character or substring.
  • e.g.: #include?, #start_with?. #end_with?, #index, etc...

case change methods

  • e.g.: #upcase, #downcase, #swapcase, #capitalize, etc...

split methods

  • split strings by particular characters.
  • e.g. combine #split and regex to operate.
  • like split strings on newlines, and parse date in csv.

concatenate methods

  • create new string by adding two string together.
  • efficiency: '<<' same as '#concat' better than '+'
  • << and #concat change the original string object.

replace substring

  • first search for substrings or use regex.
  • e.g.: #sub(a, b); #gsub(a, b); #gsub(/regex/, b);
  • regex are a concise and flex means for 'matching' strings.
  • if you want to implement a parser, #match might be a good friend :P

chop and chomp

  • todo
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 1.改变string的输出格式但不覆盖 a = "Ho! " a.size#=> 4 a * 3#=> "Ho! ...
    dingfj阅读 3,294评论 0 0
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 12,871评论 0 6
  • 把 OC 中基本数据存储到沙盒中,被称为基本数据持久化。 基本数据,在 oc中有以下四种,分别为 NSString...
    flyrees阅读 4,889评论 0 0
  • 有些人在你生命中只是昙花一现, 而你却终身守候。 真正爱上一个人的时候, 一切都那么值得, 包括不可避免的伤害。 ...
    思考Ajax阅读 1,813评论 0 1