本来是想写到 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
# 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"
# Python3
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-6]
'hello'
# 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'
# 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
# Ruby
[41] pry(main)> "hello".center 10
=> " hello "
# Python3
>>> "hello".center(10)
' hello '
# 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'
# Ruby
[50] pry(main)> "hello".chop
=> "hell"
[51] pry(main)> "hello".chop!
=> "hell"
# Python3
>>> "hello\n"[:-1]
'hello'
# 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'
# Ruby
[54] pry(main)> "hello".count 'l'
=> 2
# Python3
>>> "hello".count('l')
2
# Ruby
[55] pry(main)> "hello".crypt "world"
=> "woglQSsVNh3SM"
# Ruby
[57] pry(main)> "hello".delete "ho"
=> "ell"
# Python3
>>> "hello".replace("h", "").replace("o", "")
'ell'
# Ruby
[58] pry(main)> "Hello".downcase
=> "hello"
# Python3
>>> "Hello".lower()
'hello'
# Ruby
[59] pry(main)> "Hello".dump
=> "\"Hello\""
# 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
# 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]
# Ruby
[69] pry(main)> "hello".empty?
=> false
[70] pry(main)> "".empty?
=> true
# Python3
>>> "hello" == ""
False
>>> "" == ""
True
# 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
# Ruby
[71] pry(main)> "hello".eql? "hello"
=> true
# Python3
>>> aa = ['hello', 'world']
>>> bb = ['hello', 'world']
>>> aa == bb
True
>>> aa is bb
False
# Ruby
[76] pry(main)> "hello".hash
=> 4132711420684197129
# Python3
>>> hash("hello")
-7700562595699703259
# Ruby
[77] pry(main)> "hello".hex
=> 0
[78] pry(main)> "afcde".hex
=> 720094
# 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 "", line 1, in
ValueError: substring not found
>>>
# Ruby
[87] pry(main)> "hello".insert(3, '---')
=> "hel---lo"
# Python3
# Python3 里边字符串是不可改变的,要想达到效果,可剑走偏锋
>>> aa = "hello"
>>> aa[:3] + '---' + aa[3:]
'hel---lo'
# Ruby
[88] pry(main)> "hello".to_sym
=> :hello
# Ruby
[89] pry(main)> "hello".size
=> 5
[90] pry(main)> "hello".length
=> 5
# Python3
>>> len("hello")
5
# Ruby
[91] pry(main)> "hello".ljust(10, '-')
=> "hello-----"
[92] pry(main)> "hello".rjust(10, '-')
=> "-----hello"
# Python3
>>> "hello".ljust(10, '-')
'hello-----'
# Ruby
[93] pry(main)> " hello".lstrip
=> "hello"
[93] pry(main)> " hello".lstrip!
=> "hello"
# Python3
>>> " hello".lstrip()
'hello'
# Ruby
[94] pry(main)> "hello ".rstrip
=> "hello"
[95] pry(main)> "hello ".rstrip!
=> "hello"
# Python3
>>> "hello ".rstrip()
'hello'
# Ruby
[97] pry(main)> " hello ".strip
=> "hello"
[98] pry(main)> " hello ".strip!
=> "hello"
# Python3
>>> " hello ".strip()
'hello'
# 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'
# 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'
>>>
# Ruby
[106] pry(main)> "hello".sum
=> 532
[107] pry(main)> "hello".sum(n=16)
=> 532
# Ruby
[108] pry(main)> "HeLLo".swapcase
=> "hEllO"
[109] pry(main)> "HeLLo".swapcase!
=> "hEllO"
# 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 "", line 1, in
ValueError: could not convert string to float: 'HeLLo'
>>> float("12.34")
12.34
# 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 "", line 1, in
ValueError: invalid literal for int() with base 10: '12.34'
>>> int("12")
12
# Ruby
[120] pry(main)> "hello".to_s
=> "hello"
[121] pry(main)> "hello".to_str
=> "hello"
# Python3
>>> str("hello")
'hello'
# 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"
# 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"
# Ruby
[138] pry(main)> "Hello".upcase
=> "HELLO"
[139] pry(main)> "Hello".upcase!
=> "HELLO"
# Python3
>>> "Hello".upper()
'HELLO'
# Ruby
[131] pry(main)> "Hello".upto("Hellz"){|s| puts s}
Hello
Hellp
Hellq
Hellr
Hells
Hellt
Hellu
Hellv
Hellw
Hellx
Helly
Hellz
=> "Hello"
# Ruby
[3] pry(main)> %w[hello world]
=> ["hello", "world"]
to be continued
# Ruby
# Python3