#定义字符串
"abc"
'efg'
%Q{abc} #等价于 ""
%{hahaha} #等价于 ""
%q!efg! #等价于 ''
%!hello! #等价于 ''
Q:"" 和'' 两者之间的区别?
A:"" 中可以嵌入#{}输出表达式的值,或者是character escapes
str = "abc"
puts "this is #{str}" # this is abc
puts 'this is #{str}' # this is #{str}
puts "abc\nedf" # abc
# edf
puts 'abc\nefg' # abc\nefg
Q:ruby中字符编码?
Q:在ruby中puts "中國" 出现"invalid multitype char<us-ASCII>"
A:ruby中的source code 默認的編碼是US-ACSII,不能解析中文。需要把源文件的編碼改為“適合”的方式,繁體是
#encoding: Big5
#new
my_name = "tony"
your_name = String.new(my_name)
puts my_name.object_id #5907780
puts your_name.object_id #5907768
your_name[0] = "m"
puts my_name # tony
puts your_name # mony
#String.try_convert
puts "try_convert"
puts String.try_convert("aBC") #aBC
puts String.try_convert(12345) #nil
puts String.try_convert("12345") #12345
# % format string
puts "%"
puts "str % arg -> string"
puts "%05d" % 123 #00123 formatstring = "%flag with precision name fieldtype"
puts "%0b" % 123 #1111011
puts "%5s: %08x" % ["ID", self.object_id] # ID: 005af846
puts "%-5s: %08X" % ["ID", self.object_id] #ID : 005AF846
puts "%-5<name>s : %08<value>X" % {name: "hash_id", value: self.object_id} #hash_id: 005AF86
# * copies
# + concatenation
# << append if the object is a Fixnum it is considered to be a codepoint in the encoding
# of str and converted to the appropriate character before being appended
# concat
puts "Ha" * 3 #HaHaHa
puts "Ha" + "Ho" #HaHo
puts "Hello" << 119 #Hellow
puts "Hello" << "Tony" #HelloTony
puts "concat"
puts "Hello".concat(" Tony") # Hello Tony
str = "hello"
str << 119
puts str << "Tony" #hellowTony
#[] []=
puts "[],[]="
str = "Hello Ruby"
puts str[-1] # y
puts str[0] # H
puts str[1] #e
puts str[9] #y
puts str[10] #nil
puts str[0..2] #hel
puts str[0...2] #he
puts str[0,2] #he
puts str["e"] #e
str[0..2] = 'haha'
puts str # hahalo Ruby
#ascii_only?
puts "ascii_only?"
puts __ENCODING__ #Big5
puts "ruby".ascii_only? #true
str = "中國"
puts str.ascii_only? #false
#bytes, chars, codepoints
#str.bytes -> enum
#str.bytes{|byte| block} -> str
#str.chars -> enum
#str.chars{|char| block}-> str
#codepoints(integers representation of the characters)
#str.codepoints -> enum
#str.codepoints {|integer| block} -> str
#getbyte
puts "bytes, getbyte"
p "ruby".bytes.to_a #[114,117,98,121]
puts "getbyte"
puts "ruby".getbyte(1) #117
result = []
puts "ruby".bytes {|byte| result << byte} #ruby
p result #[114,117,98,121]
puts "chars"
p "ruby".chars.to_a # ["r", "u", "b", "y"]
result = []
puts "ruby".chars {|char| result << char} #ruby
p result # ["r", "u", "b", "y"]
puts "codepoints"
p "中國".codepoints.to_a # [42148, 45290]
result = []
puts "中國".codepoints {|b| result << b} #中國
p result #[42148, 45290]
#bytesize, length, size
puts "bytesize, length"
puts "ruby".length #4
puts "ruby".bytesize #4
puts "ruby".size #4
puts "中國".length # 2
puts "中國".bytesize # 4
puts "中國".size #2
#capitalize, capitalize!
#capitalize return a copy of str with the first character converted to
# uppcase and the remainder to lowercase
#capitalize! Modified str by converting the first character to uppercase and the
# ramainder to lowercase .Returns nil if no changes are made
puts "capitalize, capitalize! "
str = "hello Ruby"
puts "str = hello Ruby"
puts "str.capitalize = > #{str.capitalize}" #Hello ruby
puts "str = > #{str}" # hello Ruby
puts "str.capitalize! = > #{str.capitalize!}" # Hello Ruby
puts "str = > #{str}" # Hello Ruby
puts "str.capitalize! = > #{str.capitalize!}" # nil
puts "str = > #{str}" # Hello Ruby
#<=>, casecmp
puts "<=> , casecmp"
puts "abc" <=> "Abc" #1
puts "abc".casecmp("Abc") # 0
puts "abc" <=> "ab" #1
puts "abc".casecmp("ab") #1
puts "ab" <=> "abc" #-1
puts "ab".casecmp("abc") #-1
#center
puts "center"
str = "ruby"
puts str.center(4) #ruby
puts str.center(5) #ruby
puts str.center(10, '*') #***ruby***
puts str # ruby
#chr return the first character
puts "chr"
puts "ruby".chr # r
puts "中國".chr # 中
#clear removes the content(but not the associated encoding) of str
puts "clear"
str = "ruby"
puts str.clear #nil
puts str.length #0
#encoding: Big5
puts str.encoding #Big5
#chomp, chomp!
#chomp return new string with given record separator remove from the end of str
#str.chomp(rs = $/) -> string
puts "chomp, chomp!"
str = "ruby"
p str.chomp # ruby
p str.chomp("y") # rub
str = "ruby\n"
p str.chomp # ruby
str = "ruby\n\r"
p str.chomp # ruby\n
str = "ruby\r\n"
p str.chomp # ruby
str = "ru \n by"
p str.chomp # ru \n by
#chop, chop!
#remove last character
puts "chop"
p "ruby".chop #rub
p "ruby\n".chop # ruby
p "ruby\r\n".chop #ruby
p "R".chop.chop # ""
#count, delete, delete!
#str.count(<string>+)->int
#Each string parameter difines a set of characters to count.
#the intersection of these sets defines the characters to count in str
#Any parameter that starts with a caret(^) is negated
#The sequence c1-c2 means all characters between c1 and c2
puts "count"
str = "hello world"
puts str.count "lo" #5 在str 中 有 3 個 l 和 兩個 0 => 3 + 2 = 5
puts str.count "el" #4
puts str.count "lo", "o" #2 [l,o],[o]的交集是 0 在str中出現2個
puts str.count "ej-m" # 4 [e,j,k,l,m] 在str 中有 1個 e 3個l 總共 4
puts "delete"
puts str.delete "lo" # he wrd
puts str # hello world
puts str.delete! "e" # hllo world
puts str # hllo world
#crypt 加密
puts "crypt"
puts "ruby".crypt "ruby"
#downcase, downcase!
puts "downcase downcase!"
puts "ABC".downcase #abc
#dump
#Produces a version of str with all nonprinting characters replaced by \nnn notation
#and all special characters escaped
puts "dump"
p "ABC".dump #"\"ABC"\"
p "ABC" #"ABC"
#empty?
puts "empty?"
puts "hello".empty? # false
puts "".empty? #true
#encode, encode!, encoding, force_encoding
puts "encoding"
str = "ruby"
puts str.encoding #GBK
puts str.encode("utf-8") # ruby
puts str.encoding #GBK
puts str.encode!("utf-8") # ruby
puts str.encoding #utf-8
str.force_encoding("GBK")
puts str.encoding #GBK
#gsub, gsub! 與正則表達式連用搜索字符串
#start_with? end_with?
puts "start_with?, end_with"
puts "Apache".end_with?("ache") # true
puts "ruby code".end_with?("python", "perl", "code") # true
puts "Apache".start_with?("Apa") # true
puts "ruby code".start_with?("python","perl","ruby") #true
#index
puts "index"
puts "hello".index("e") #1
puts "hello".index("a") #nil
puts "hello".index(/[aeiou]/, -3) #4
puts "hello".index("lo",4) #nil 第二參數開始搜索的位置
#insert
puts "insert"
puts "abcd".insert(0,"X") #Xabcd
puts "abcd".insert(-1, "X") #abcdX
puts "abcd".insert(1, "X") #aXbcd
puts "abcd".insert(-2, "X") #abcXd
#intern
#Return the Symbol corresponding to str, creatin the symbol if it did not
#previously exist
puts "intern"
p "ruby".intern #:ruby
分享到:
相关推荐
本资源是ruby代码,提供了一系列封装好的函数,用于快速进行转换,一个函数搞定,包括如下转换,二进制字符串与hex字符串的互转。二进制字符串与整数互转,包括uint8,uin16,uint32, 以及本地字节序和网络字节序两种...
Ruby将字符串像数字一样处理.我们用单引号(‘…’)或双引号(…)将它们括起来. ruby> abc abc ruby> ‘abc’ abc 单引号和双引号在某些情况下有不同的作用.一个由双引号括起来的字符串允许字符由一个前置的斜杠...
2.判断字符串中是否包含另一个串 代码如下: str.include? other_str => true or false “hello”.include? “lo” #=> true “hello”.include? “ol” #=> false “hello”.include? ?h #=> true 3.字符串插入 ...
5. 什么是Ruby中的字符串(String)操作?请说明在Ruby中的字符串常用操作方法。 6. 在Ruby中如何定义函数(Method)?请简要介绍在Ruby中的函数定义和调用方式。 7. Ruby中如何处理异常(Exception Handling)?请...
什么是模糊字符串匹配 Fuzzy-string-match是用于ruby的模糊字符串匹配库。... 纯Ruby版本可以处理ASCII和UTF8字符串。 (而且很慢) 本机版本只能使用ASCII字符串。 (但是很快) 样例代码 本机版本 req
如果你从 Ruby 或者 Python 转型到 Go,将会有很多语言差异需要学习,其中很多问题都是围绕处理 string 类型。 下面是一些字符串的技巧,这些技巧解决了我在使用 Golang 的最初几周中遇到的问题。 1. 多行字符串 ...
受 Java、Ruby 和 Python 等其他高级语言的启发,我开发了一组字符串操作函数,以便更方便地处理字符串。 这里给出了一个简要的内容列表 strjoin:使用用户指定的分隔符连接多个字符串 strsplit:从字符串中提取由...
主要介绍了Ruby中操作字符串的一些基本方法,包括对字符串的压缩和解压缩等处理,需要的朋友可以参考下
该程序可能使用三种方法编码器:一种通过基于“移位器”“移位”用户生成的字符串中的每个字母来对用户字符串进行编码的方法解码器:一种通过了解移位器将编码后的密码解码回用户字符串的方法。 SHIFTER:一种随机...
LetterAvatar 从用户名(或其他字符串)创建字母头像的 gem
pack模板字符串 sprintf格式 Marshal格式 Ruby FAQ Ruby的陷阱 Ruby/Tk FAQ Ruby的相关书籍 Ruby Documentation Project (RDP) HTML Help版和分立HTML ReFe 索引 功能分类索引 发布条件
强大的字符串操作和正则表达式 开发中快速回馈 迅速和简便: 无需变量声明 变量无类型 语法简单而坚实 自动内存管理 面向对象编程 任何事物都是一个对象 类,继承,方法,等等 单态方法 模块糅合 ...
Ruby 字符串 常用缩略词 CSV:逗号分隔值 REXML:Ruby Electric XML XML:可扩展标记语言 Ruby 中的 String 是容纳、比较和操作文本数据的一种强大方法。在 Ruby 中,String 是一个类,可以通过调用 String::new ...
8.2对字符串和块进行求值268 8.3变量和常量271 8.4方法272 8.5钩子方法277 8.6跟踪279 8.7OBJECTSPACE和GC281 8.8定制控制结构281 8.9缺失的方法和常量284 8.10动态创建方法287 8.11别名链290 ...
尤其是当你处理预定义好的较长字符串时,把这种字符串分成多行书写更有助于提高代码的可读性和可维护性.在一些语言中,多行字符串还可以用来做代码注释.大部分动态脚本语言都支持多行字符串,比如Python, Ruby, PHP. 但...
字符串 正则表达式 数组 再读“简单示例” 流程控制 迭代器 面向对象的思考 方法 类 继承 重新定义方法 访问控制 单例方法 模块 过程对象 变量 全局变量 实例变量 局部变量 类常量 异常处理:...
处理简单字符串的流行解决方案在简单情况下效果很好。(对不起;-)使用更复杂的字符串,您可能会得到意想不到的结果: ActiveSupport :: Inflector . underscore ( "supports IPv6 on iOS 14.4?" ) # => "supports ...
这两种形式的区别在于,当构造字面量时,Ruby对字符串所做处理的多少有所不同。Ruby对单引号串处理得很少。除了极少的一些例外。键入到字符串字面量的内容就构成了这个字符串的值。 Ruby对双引号字符串有更多的处理...
Redis数据结构和操作 redis不只是一个简单的键(key)-值(value)数据库,实际上它是一... 位数组(位图bitmaps):可以通过特殊命令,像处理位图 一样地处理字符串:设置和清除某一位,统计被置1的位数, 找到第一个被设
比如 代码如下: str1=”abc” str2=”abc” 在java中,对于字面量的字符串,jvm内部维持一张表,因此如果在java中,str1和str2是同一个String对象。而在Ruby中, str1和str2是完全不同的对象。同样,在java中对于...