I have a string like so string1
:
Hello World, join my game:
I would like to make string1 become:
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?
THanks
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
You can add line feed in string simply by adding “\r\n” or “\n”.
CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.
to give new line as input we will use \n(enter) when the input end carriage return come to play which is (\r).
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.erb
or .rhtml
document:
<%= "Hello<br /><br />#{myURL}" %> # or link_to helper
If you already have a string like string1
then you can append to it using either +=
or <<
:
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
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
[string1, "\n", string2].join
OR
[string1, string2].join("\n")
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.
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 puts
on the output Array:
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:
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:
puts output.join("\n\n")
#=> Hello World, join my game:
#=> http://game.com/url
#=> Thank You!
As far as I know there is no new line constant. Use escape sequence '\n'. like:
puts "1. Hello\n2. World"
Ref: http://en.wikibooks.org/wiki/Ruby_Programming/Strings
If you are using puts statements, a simple way to print on new lines is as follows:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With