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

若如初见. 提交于 2020-08-15 04:19:46

本来是想写到 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'

str * 3

# 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'

str + other_str

# 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
# 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'>

str.sub_str

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

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.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\n"[:-1]
'hello'

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.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_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.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.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.to_sym

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

str.size(str.length)

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

str.ljust(str.rjust)

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

str.lstrip(str.lstrip!)

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

str.rstrip(str.rstrip!)

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

str.strip(str.strip!)

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

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"
# Python3
>>> "hello world".replace("world", "hello")
'hello hello'

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"

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.tr(str.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"]

to be continued 

# Ruby
# Python3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!