Ruby-on-rails 给定一个字符串,如何在回车后附加另一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10033452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Given a string, how to append a carriage return followed by another string?
提问by AnApprentice
I have a string like so string1:
我有一个像这样的字符串string1:
Hello World, join my game:
I would like to make string1 become:
我想让 string1 变成:
Hello World, join my game:
http://game.com/url
How can I append a carriage return with ruby and then a link from another variable?
如何使用 ruby 附加回车符,然后附加来自另一个变量的链接?
THanks
谢谢
回答by Charles Caldwell
It really depends on what you are outputting to.
这实际上取决于您要输出的内容。
$STDOUT:
$标准输出:
puts "Hello\n\n#{myURL}"
or
或者
puts "Hello"
puts
puts myURL
or
或者
puts <<EOF
Hello
#{myURL}
EOF
If you are outputting this in an html.erbor .rhtmldocument:
如果您在html.erbor.rhtml文档中输出它:
<%= "Hello<br /><br />#{myURL}" %> # or link_to helper
If you already have a string like string1then you can append to it using either +=or <<:
如果您已经有一个字符串,string1那么您可以使用+=或附加到它<<:
string1 = "Hello world, join my game:"
myUrl = "http://example.com"
string1 += "\n\n#{myUrl}"
or:
或者:
string1 = "Hello world, join my game:"
myUrl = "http://example.com"
string +=<<EOF
#{myUrl}
Here's some other details
EOF
回答by MrTheWalrus
Assuming you have these strings:
假设你有这些字符串:
string1 = 'foo'
string2 = 'bar'
Here's three ways to combine them with a newline in between:
以下是将它们与它们之间的换行符组合在一起的三种方法:
String Interpolation:
字符串插值:
"#{string1}\n#{string2}"
'+' Operator:
“+”运算符:
string1 + "\n" + string2
Array and .join
数组和 .join
[string1, "\n", string2].join
OR
或者
[string1, string2].join("\n")
回答by ficuscr
As far as I know there is no new line constant. Use escape sequence '\n'. like:
据我所知,没有新的行常量。使用转义序列“\n”。喜欢:
puts "1. Hello\n2. World"
回答by Steve Karaga
If you are using puts statements, a simple way to print on new lines is as follows:
如果您使用 puts 语句,在新行上打印的简单方法如下:
puts "Hello, here is the output on line1", "followed by some output on line2"
This will return:
这将返回:
Hello, here is the output on line1
followed by some output on line2
if you run the code in the irb in terminal.
如果您在终端的 irb 中运行代码。
回答by Joshua Pinter
Output as an Array.
输出为数组。
If you're appending multiple times to the same String variable and you want to later output each of these appended Strings in new lines, you might want to consider using an Array to store each String in and then just join them with a new line when displaying/outputting them.
如果您多次附加到同一个 String 变量,并且您想稍后在新行中输出这些附加字符串中的每一个,您可能需要考虑使用 Array 来存储每个字符串,然后在以下情况下将它们与新行连接起来显示/输出它们。
output = []
output << "Hello World, join my game:"
output << "http://game.com/url"
output << "Thank You!"
Now, if you're in terminal or something, you can just use putson the output Array:
现在,如果你在终端或其他地方,你可以puts在输出数组上使用:
puts output
#=> Hello World, join my game:
#=> http://game.com/url
#=> Thank You!
If you want to display it in HTML you might want to use the join()method:
如果你想在 HTML 中显示它,你可能需要使用以下join()方法:
output.join('<br>')
#=> "Hello World, join my game:<br>http://game.com/url<br>Thank You!"
You can use whatever you like in the join()method, so if you wanted to have two line breaks between each string, you could do this:
您可以在该join()方法中使用任何您喜欢的内容,因此如果您想在每个字符串之间有两个换行符,您可以这样做:
puts output.join("\n\n")
#=> Hello World, join my game:
#=> http://game.com/url
#=> Thank You!

