本来是想写到学习笔记一块的,但是字符串操作这块的内容确实太多了,所以单独提取出来了。
Ruby 和 Python 两种语言都支持单引号和双引号的字符串。话不多说,直接上菜
字符串中嵌入变量
# 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'
to be continued
# Ruby
# Python3