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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,692評論 19 139
  • 1.改變string的輸出格式但不覆蓋 a = "Ho! " a.size#=> 4 a * 3#=> "Ho! ...
    dingfj閱讀 523評論 0 0
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,067評論 0 6
  • 把 OC 中基本數(shù)據(jù)存儲到沙盒中,被稱為基本數(shù)據(jù)持久化。 基本數(shù)據(jù),在 oc中有以下四種,分別為 NSString...
    flyrees閱讀 1,254評論 0 0
  • 有些人在你生命中只是曇花一現(xiàn), 而你卻終身守候。 真正愛上一個人的時候, 一切都那么值得, 包括不可避免的傷害。 ...
    思考Ajax閱讀 318評論 0 1

友情鏈接更多精彩內(nèi)容