Ruby 字符串操作(和 Python3 字符串操作进行对比)

本来是想写到 Ruby 学习笔记一块的,但是字符串操作这块的内容确实太多了,所以单独提取出来了。

Ruby 和 Python 两种语言都支持单引号和双引号的字符串。话不多说,直接上菜(为了对比明显,Ruby 中尽量去掉括号)!

字符串中嵌入变量

# Ruby
[1] pry(main)> name1 = "Joe"
=> "Joe"
[2] pry(main)> name2 = "Mary"
=> "Mary"
[3] pry(main)> puts "你好 #{name1},  #{name2} 在哪?"
你好 Joe,  Mary 在哪?
=> nil
# Python3
>>> name1 = "Joe"
>>> name2 = "Mary"
>>> print(f"你好 {name1}, {name2} 在哪?")
你好 Joe, Mary 在哪?

 字符串内建方法

# Ruby
[4] pry(main)> myStr = String.new("THIS IS TEST")
=> "THIS IS TEST"
[5] pry(main)> foo = myStr.downcase
=> "this is test"
[6] pry(main)>  
[7] pry(main)> puts "#{foo}"
this is test
=> nil
[8] pry(main)> 
# Python3
>>> myStr = str("THIS IS TEST")
>>> myStr
'THIS IS TEST'
>>> foo = myStr.lower()
>>> foo
'this is test'

字符串自我复制

# Ruby
[9] pry(main)> aa = "hello world"
=> "hello world"
[10] pry(main)> aa * 3
=> "hello worldhello worldhello world"
# Python3
>>> aa = "hello world"
>>> aa * 3
'hello worldhello worldhello world'

字符串拼接

# Ruby
[11] pry(main)> "hello" + " world"
=> "hello world"
[12] pry(main)> "hello" << " world"
=> "hello world"
# Python3
>>> "hello" + " world"
'hello world'

字符串比较

# Ruby
[13] pry(main)> "hello" < "world"
=> true
[14] pry(main)> "hello" <=> "world"
=> -1
[15] pry(main)> "hello" == "world"
=> false
# Python3
>>> "hello" < "world"
True
>>> 0 if "hello" == "world" else (-1 if "hello" < "world" else 1)
-1
>>> "hello" == "world"
False

正则表达式匹配

# Ruby
[18] pry(main)> "hello world" =~ /ll/
=> 2
[19] pry(main)> "hello world" =~ /o.*o/
=> 4
[5] pry(main)> "hello world".match(/o.*o/)
=> #<MatchData "o wo">
# Python3
>>> import re
>>> re.search("ll", "hello world")
<_sre.SRE_Match object; span=(2, 4), match='ll'>
>>> re.search("o.*o", "hello world")
<_sre.SRE_Match object; span=(4, 8), match='o wo'>

字符串子串

# Ruby
[19] pry(main)> "hello world"[0,5]
=> "hello"
[20] pry(main)> "hello world"[0...5]
=> "hello"
[21] pry(main)> "hello world"[/o.*o/]
=> "o wo"
# Python3
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-6]
'hello'

字符和 ASCII 相互转换 

# Ruby
irb(main):001:0> 97.chr
=> "a"
irb(main):002:0> "a".ord
=> 97
# Python
>>> chr(97)
'a'                                                                          
>>> ord('a')                                                                 
97

str.capitalize(str.capitalize!)

# Ruby
[27] pry(main)> aa = "hello world"
=> "hello world"
[28] pry(main)> aa.capitalize
=> "Hello world"
[29] pry(main)> aa
=> "hello world"
[30] pry(main)> aa.capitalize!
=> "Hello world"
[31] pry(main)> 
# Python3
>>> aa = "hello world"
>>> aa.capitalize()
'Hello world'
>>> aa
'hello world'
>>> aa = aa.capitalize()
>>> aa
'Hello world'

str.casecmp(不区分大小写的字符串比较)

# Ruby
[34] pry(main)> "hello".casecmp "He5l5"
=> 1
[35] pry(main)> "hello".casecmp "HeLLo"
=> 0
[36] pry(main)> "hello".casecmp "world"
=> -1
# Python3
>>> "hello".lower() == "HeLLo".lower()
True

str.center

# Ruby
[41] pry(main)> "hello".center 10
=> "  hello   "
# Python3
>>> "hello".center(10)
'  hello   '

str.chars

# Ruby
[2] pry(main)> "hello".chars
=> ["h", "e", "l", "l", "o"]
or
[5] pry(main)> hello = []
=> []
[6] pry(main)> "hello".each_byte{|item| hello << item.chr}
=> "hello"
[7] pry(main)> hello
=> ["h", "e", "l", "l", "o"]
# Python3
>>> hello = "hello"
>>> hello = list("hello")
>>> hello
['h', 'e', 'l', 'l', 'o']

str.chomp(str.chomp!)

# Ruby
[46] pry(main)> "hello\n".chomp!
=> "hello"
[47] pry(main)> "hello".chomp!
=> nil
[48] pry(main)> "hello\n".strip!
=> "hello"
[49] pry(main)> "hello".strip!
=> nil
# Python3
>>> "hello\n".strip()
'hello'

 str.chop(str.chop!)

# Ruby
[50] pry(main)> "hello".chop
=> "hell"
[51] pry(main)> "hello".chop!
=> "hell"
# Python3
>>> "hello"[:-1]
'hell'

str.concat(other_str)

# Ruby
[53] pry(main)> "hello".concat " world"
=> "hello world"
[3] pry(main)> aa = "hello"
=> "hello"
[4] pry(main)> aa.concat(" world")
=> "hello world"
[5] pry(main)> aa
=> "hello world"

# Python3
>>> aa = "hello"
>>> aa += " world"
>>> aa
'hello world'

str.count

# Ruby
[54] pry(main)> "hello".count 'l'
=> 2
# Python3
>>> "hello".count('l')
2

str.crypt

# Ruby
[55] pry(main)> "hello".crypt "world"
=> "woglQSsVNh3SM"

str.delete(str.delete!)

字符串中出现在后面参数中的每个字符都会从原字符串中删除。

# Ruby
[57] pry(main)> "hello".delete "ho"
=> "ell"
# Python3
>>> "hello".replace("h", "").replace("o", "")
'ell'

str.delete_ prefix(str.delete_ prefix!)

# Ruby
[1] pry(main)> aa = "hello hello world"
=> "hello hello world"
[2] pry(main)> aa.delete_prefix "hello"
=> " hello world"
[3] pry(main)> aa
=> "hello hello world"
[4] pry(main)> aa.delete_prefix! "hello"
=> " hello world"
[5] pry(main)> aa
=> " hello world"
# Python3
>>> aa = "hello hello world"
>>> aa
'hello hello world'
>>> aa.replace("hello", "", 1) if aa.startswith("hello") else aa
' hello world'

str.delete_ suffix(str.delete_ suffix!)

# Ruby
[6] pry(main)> aa = "hello world world"
=> "hello world world"
[7] pry(main)> aa.delete_suffix "world"
=> "hello world "
[8] pry(main)> aa
=> "hello world world"
[9] pry(main)> aa.delete_suffix! "world"
=> "hello world "
[10] pry(main)> aa
=> "hello world "
# Python3
>>> aa = "hello world world"
>>> aa
'hello world world'
>>> aa[:aa.rindex("world")] if aa.endswith("world") else aa
'hello world '

str.downcase(str.downcase!)

# Ruby
[58] pry(main)> "Hello".downcase
=> "hello"
# Python3
>>> "Hello".lower()
'hello'

str.dump

# Ruby
[59] pry(main)> "Hello".dump
=> "\"Hello\""

str.each_byte

# Ruby
[64] pry(main)> "Hello world".each_byte{|substr| puts substr}
72
101
108
108
111
32
119
111
114
108
100
# Python3
>>> [print(ord(item)) for item in "Hello world"]
72
101
108
108
111
32
119
111
114
108
100

str.each_char

# Ruby
[48] pry(main)> "hello".each_char{|char| puts char}
h
e
l
l
o
=> "hello"
# Python3
>>> [print(char) for char in "hello"]
h
e
l
l
o
[None, None, None, None, None]

str.each_line

# Ruby
[67] pry(main)> "hello\nworld".each_line{|substr| puts substr}
hello
world
=> "hello\nworld"
# Python3
>>> [print(item) for item in "hello\nworld".split()]
hello
world
[None, None]

str.empty?

# Ruby
[69] pry(main)> "hello".empty?
=> false
[70] pry(main)> "".empty?
=> true
# Python3
>>> "hello" == ""
False
>>> "" == ""
True

str.equal?

# Ruby
[73] pry(main)> "hello".equal? "hello"
=> false
[74] pry(main)> aa = "hello" 
=> "hello"
[75] pry(main)> aa.equal? aa
=> true
# Python3
>>> "hello" is "hello"
True
>>> "hello" == "hello"
True

>>> aa = ['hello', 'world']
>>> bb = ['hello', 'world']
>>> aa == bb
True
>>> aa is bb
False

str.eql?

# Ruby
[71] pry(main)> "hello".eql? "hello"
=> true
# Python3
>>> aa = ['hello', 'world']
>>> bb = ['hello', 'world']
>>> aa == bb
True
>>> aa is bb
False

str.gsub(pattern, replacement) [or] str.gsub(pattern) { |match| block }

# Ruby
[9] pry(main)> "hello world".gsub(/o.*o/, "*")
=> "hell*rld"
[10] pry(main)> "hello world".gsub(/o.*o/){|match| "*"}
=> "hell*rld"
[11] pry(main)> "hello world".gsub("l", "*")
=> "he**o wor*d"
# Python3
>>> "hello world".replace("l", "*")
'he**o wor*d'

str.hash

# Ruby
[76] pry(main)> "hello".hash
=> 4132711420684197129
# Python3
>>> hash("hello")
-7700562595699703259

str.hex

# Ruby
[77] pry(main)> "hello".hex
=> 0
[78] pry(main)> "afcde".hex
=> 720094

str.include?

# Ruby
[2] pry(main)> "hello world".include?"hello"
=> true
# Python3
>>> "hello" in "hello world"
True

str.index

# Ruby
[84] pry(main)> "hello".index("e")
=> 1
[85] pry(main)> "hello".index("ll")
=> 2
[86] pry(main)> "hello world".index(/o.*o/)
=> 4
# Python3
>>> "hello".index("e")
1
>>> "hello".index("ll")
2
>>> "hello world".index("o.*o")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> 

str.insert

# Ruby
[87] pry(main)> "hello".insert(3, '---')
=> "hel---lo"
# Python3
# Python3 里边字符串是不可改变的,要想达到效果,可剑走偏锋
>>> aa = "hello"
>>> aa[:3] + '---' + aa[3:]
'hel---lo'

str.lines

# Ruby
[1] pry(main)> "hello world".lines
=> ["hello world"]
[2] pry(main)> "hello\nworld".lines
=> ["hello\n", "world"]
# Python3
>>> "hello world".splitlines(keepends=True)
['hello world']
>>> "hello\nworld".splitlines(keepends=True)
['hello\n', 'world']

str.ljust(str.rjust)

# Ruby
[91] pry(main)> "hello".ljust(10, '-')
=> "hello-----"
[92] pry(main)> "hello".rjust(10, '-')
=> "-----hello"
# Python3
>>> "hello".ljust(10, '-')
'hello-----'
>>> "hello".rjust(10, '-')
'-----hello'

str.lstrip(str.lstrip!)

# Ruby
[93] pry(main)> "  hello".lstrip
=> "hello"
[93] pry(main)> "  hello".lstrip!
=> "hello"
# Python3
>>> "  hello".lstrip()
'hello'

str.replace

# Ruby
[6] pry(main)> "hello".replace("world")
=> "world"
# Python3
>>> "hello".replace("hello", "world")
'world'

str.reverse(str.reverse!)

# Ruby
[7] pry(main)> "hello world".reverse
=> "dlrow olleh"
# Python3
>>> "".join(list(reversed("hello world")))
'dlrow olleh'

str.rstrip(str.rstrip!)

# Ruby
[94] pry(main)> "hello  ".rstrip
=> "hello"
[95] pry(main)> "hello  ".rstrip!
=> "hello"
# Python3
>>> "hello  ".rstrip()
'hello'

str.size(str.length)

# Ruby
[89] pry(main)> "hello".size
=> 5
[90] pry(main)> "hello".length
=> 5
# Python3
>>> len("hello")
5

str.strip(str.strip!)

# Ruby
[97] pry(main)> "  hello  ".strip
=> "hello"
[98] pry(main)> "  hello  ".strip!
=> "hello"
# Python3
>>> "  hello  ".strip()
'hello'

str.start_with? (str.end_with?)

# Ruby
[7] pry(main)> "hello".start_with? "he"
=> true
[8] pry(main)> "hello".start_with? "te"
=> false
[9] pry(main)> "hello".end_with? "lo"
=> true
[10] pry(main)> "hello".end_with? "to"
=> false
# Python3
>>> "hello".startswith("he")
True
>>> "hello".startswith("te")
False
>>> "hello".endswith("lo")
True
>>> "hello".endswith("to")
False

str.scan

# Ruby
[11] pry(main)> "hello world hello world".scan(/o.*?o/)
=> ["o wo", "o wo"]
# Python3
>>> re.findall("o.*?o", "hello world hello world")
['o wo', 'o wo']

str.split

如果 pattern 是一个单一的空格,那么 str 是基于空格进行分割,会忽略前导空格和连续空格字符。

# Ruby
[19] pry(main)> "hello world".split "l"
=> ["he", "", "o wor", "d"]
[1] pry(main)> "I    am   Looking".split
=> ["I", "am", "Looking"]
[2] pry(main)> "I    am   Looking".split(' ')
=> ["I", "am", "Looking"]

# 分隔的符号也可以不止一个
[19] pry(main)> "hello world".split(/o|l/)
=> ["he", "", "", " w", "r", "d"]
# Python3
>>> "hello world".split("l")
['he', '', 'o wor', 'd']
>>> "I    am   Looking".split()
['I', 'am', 'Looking']
>>> "I    am   Looking".split(' ')
['I', '', '', '', 'am', '', '', 'Looking']

str.squeeze([other_str]*) [or] str.squeeze!([other_str]*)

使用为 String#count 描述的程序从 other_str 参数建立一系列字符。返回一个新的字符串,其中集合中出现的相同的字符会被替换为单个字符。如果没有给出参数,则所有相同的字符都被替换为单个字符。

简单理解就是:str 中的连续相同字符 char 如果在 str.chars & other_str.chars 里边,那么它只在 str 出现一次。

没有 other_str 的话,默认把左右连续相同字符都变成一个。

# Ruby
[31] pry(main)> "hello".squeeze("world")
=> "helo"
[32] pry(main)> "heello".squeeze("world")
=> "heelo"
[47] pry(main)> "hetello".squeeze("world")
=> "hetelo"
[57] pry(main)> "helllo world".squeeze
=> "helo world"
# Python3

str.sub(str.sub!)

# Ruby
[99] pry(main)> "hello world".sub("world", "hello")
=> "hello hello"
[100] pry(main)> "hello world".sub!("world", "hello")
=> "hello hello"
[102] pry(main)> "hello world".sub("world"){|match| "hello"}
=> "hello hello"
[103] pry(main)> "hello world".sub("world"){"hello"}
=> "hello hello"

# 看下边,只替换了一处哟,和 gsub 的区别应该在于 gsub 的 g 代表 global,即全局替换。
[14] pry(main)> "hello world".sub("l", "*")
=> "he*lo world"
# Python3
>>> "hello world".replace("l", "*", 1)
'he*lo world'

str.succ,str.next(str.succ!, str.next!)

# Ruby
[104] pry(main)> "hello".succ
=> "hellp"
[105] pry(main)> "hello".next
=> "hellp"
# Python3
# 你就当成是字符串的 26 进制加法,这儿只是其中一步,整个是不对的哟
# 后边如果有时间我可以用 Python3 写个实现上面的方法
>>> aa = "hello"
>>> bb = aa[:-1] + chr(ord(aa[-1]) + 1)
>>> bb
'hellp'
>>> 

str.sum

# Ruby
[106] pry(main)> "hello".sum
=> 532
[107] pry(main)> "hello".sum(n=16)
=> 532

str.swapcase(str.swapcase!)

# Ruby
[108] pry(main)> "HeLLo".swapcase
=> "hEllO"
[109] pry(main)> "HeLLo".swapcase!
=> "hEllO"
# Python3
>>> "HeLLo".swapcase()
'hEllO

str.to_f

# Ruby
[110] pry(main)> "HeLLo".to_f
=> 0.0
[111] pry(main)> "12.34".to_f
=> 12.34
# Python3
>>> float("HeLLo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'HeLLo'
>>> float("12.34")
12.34

str.to_i

# Ruby
[118] pry(main)> "12.34".to_i
=> 12
[119] pry(main)> "a12.34".to_i
=> 0
# Python3
>>> int("12.34")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.34'
>>> int("12")
12

str.to_s(str.to_str)

# Ruby
[120] pry(main)> "hello".to_s
=> "hello"
[121] pry(main)> "hello".to_str
=> "hello"
# Python3
>>> str("hello")
'hello'

str.to_sym

# Ruby
[88] pry(main)> "hello".to_sym
=> :hello

str.tr(str.tr!)

如果你熟悉 Linux 下的 tr 命令的话,这儿就比较好理解了,tr 前后字符串的长度不会发生变化。

# Ruby
[127] pry(main)> "hello".tr("he","g")
=> "ggllo"
[128] pry(main)> "hello".tr("he","gx")
=> "gxllo"
[129] pry(main)> "hello".tr("he","gxx")
=> "gxllo"
[130] pry(main)> "hello".tr("he","gxt")
=> "gxllo"

str.tr_s(str.tr_s!)

# Ruby
[134] pry(main)> "hello".tr_s("he","g")
=> "gllo"
[135] pry(main)> "hello".tr_s("he","gg")
=> "gllo"
[136] pry(main)> "hello".tr_s("he","gx")
=> "gxllo"
[137] pry(main)> "hello".tr_s("he","gxr")
=> "gxllo"

str.upcase(str.upcase!)

# Ruby
[138] pry(main)> "Hello".upcase
=> "HELLO"
[139] pry(main)> "Hello".upcase!
=> "HELLO"
# Python3
>>> "Hello".upper()
'HELLO'

str.upto

# Ruby
[131] pry(main)> "Hello".upto("Hellz"){|s| puts s}
Hello
Hellp
Hellq
Hellr
Hells
Hellt
Hellu
Hellv
Hellw
Hellx
Helly
Hellz
=> "Hello"

骚操作哟!

%w[hello world] 

# Ruby
[3] pry(main)> %w[hello world]
=> ["hello", "world"]

%i[hello world]   

# Ruby
[11] pry(main)> %i[hello world]
=> [:hello, :world]

heredoc

无缩进

# Ruby

# test.rb
puts <<~EOF
  hello world,
  I am Looking
  Nice to meet you
EOF

------------------------------------------------------------
[root@master ruby_learning]# ruby test.rb
hello world,
I am Looking
Nice to meet you

有缩进

# Ruby

# test.rb
puts <<-EOF
  hello world,
  I am Looking
  Nice to meet you
EOF

------------------------------------------------------------
[root@master ruby_learning]# ruby test.rb
  hello world,
  I am Looking
  Nice to meet you

to be continued

# Ruby
# Python3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值