Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a string, how to append a carriage return followed by another string?

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

like image 772
AnApprentice Avatar asked Apr 05 '12 17:04

AnApprentice


People also ask

How do I insert a carriage return into a string?

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.

How do you add a new line to a string in Ruby?

You can add line feed in string simply by adding “\r\n” or “\n”.

What is string CR?

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.

What is carriage return character in Ruby?

to give new line as input we will use \n(enter) when the input end carriage return come to play which is (\r).


5 Answers

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
like image 97
Charles Caldwell Avatar answered Oct 05 '22 17:10

Charles Caldwell


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")
like image 28
MrTheWalrus Avatar answered Oct 05 '22 17:10

MrTheWalrus


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.

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!
like image 34
Joshua Pinter Avatar answered Oct 05 '22 18:10

Joshua Pinter


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

like image 25
ficuscr Avatar answered Oct 05 '22 16:10

ficuscr


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.

like image 37
Steve Karaga Avatar answered Oct 05 '22 18:10

Steve Karaga