文字列の結合
_ 文字列の結合
_ Python
+で結合。*で繰り返しになる。
>>> word = 'Help' + 'A' >>> word 'HelpA' >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>'
隣り合ったリテラル文字列は自動的に結合される。
>>> 'str' 'ing' # <- これは ok 'string' >>> 'str'.strip() + 'ing' # <- これは ok 'string' >>> 'str'.strip() 'ing' # <- これはダメ File "<stdin>", line 1, in ? 'str'.strip() 'ing' ^ SyntaxError: invalid syntax
_ Ruby
+を使う。
s = "Hello " t = s + "World"
<<を使う。
s = "Hello " s << "World"
concatを使う。
s = "Hello " s.concat("World")
<<やconcatを使うと、中身が変わるので注意。以下のコード参照
a = "AAA" b = a a << "BBB" puts b #=>AAABBB
Link: MenuBar(2472d)
Last-modified: 2017-07-19 (水) 22:33:29 (2472d)