美文网首页
Ruby: String

Ruby: String

作者: bookinstock_ | 来源:发表于2017-04-21 07:49 被阅读19次

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

相关文章

  • 字符串操作

    Ruby Methods: String#gsub ruby-methods-string-gsub senten...

  • Ruby: String

    intro strings are a sequences of characters. the name com...

  • ruby string

    1.改变string的输出格式但不覆盖 a = "Ho! " a.size#=> 4 a * 3#=> "Ho! ...

  • ruby 数据类型

    1. Ruby 字符串(String) 2. Ruby 数组 3. Ruby 哈希 哈希的内置方法 4. Ruby...

  • ruby入门

    ruby数据类型:Number、String、Bool、Array、Hash 赋值 ruby中的变量,不需要做类型...

  • sinatra 0.0.1 源码学习

    重要提醒 一定要先安装 1.8 版本的 ruby ,因为 1.9+ 的 ruby ,String 的实例是不响应 ...

  • ruby基础用法简单整理

    ruby基础用法简单整理 基础变量部分 变量声明 a = 10 a = "string" 支持并行赋值 a...

  • Ruby之单引号和双引号定义字符串

    在Ruby中 ' '和" "都可以定义字符串例如'string' "string"这两种方式定义字符串的区别...

  • Ruby的基础语法(一):基本数据类型

    Ruby支持的数据类型包括基本的Number、String、Ranges、Symbols,以及true、false...

  • ruby中symbol 和 string

    1. 了解symbol symbol是什么符号(symbol)和字符串很相似,符号也是对象,一般作为名称标签来使用...

网友评论

      本文标题:Ruby: String

      本文链接:https://www.haomeiwen.com/subject/sowrzttx.html