在写程序的时候,有时候一行字符会非常的长,比如
string1 = “string string string string string string string string string string string string string”
如果全部写在一行, 程序员都不喜欢看。
那怎么办了?
1.
str = 'this is a multi-line string'\
' using implicit concatenation'\
' to prevent spare \n\'s'
=> "this is a multi-line string using implicit concatenation to eliminate spare \\n's"
2.p <<END_SQL.gsub(/\s+/, " ").strip SELECT * FROM users ORDER BY users.id DESC END_SQL # >> "SELECT * FROM users ORDER BY users.id DESC"3.%q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7 from table1, table2, table3, etc, etc, etc, etc, etc, where etc etc etc etc etc etc etc etc etc etc etc etc etc}4.%Q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7 from #{table_names}, where etc etc etc etc etc etc etc etc etc etc etc etc etc}
本文介绍在编程中如何处理过长的字符串,通过使用多种方法将其拆分为多行以提高代码可读性。包括使用反斜杠进行隐式连接、替换空白符及利用特殊语法结构。
666

被折叠的 条评论
为什么被折叠?



