save only first line from string?

R

Robert Klemme

Hi. What's the most simple and elegant way to remove all the contents of
a String except for the first line? (Assume string consist of one line,
multiple lines, or no lines, and assume that we don't know which OS we
are on.)

irb(main):005:0> s = "multiple\nlines\n"
=> "multiple\nlines\n"
irb(main):006:0> s[/^.*$/]
=> "multiple"
irb(main):007:0> s[/^.*?$/]
=> "multiple"

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top