How to "find" new lines

D

Damir Sigur

I am new to ruby, and was trying to make a small code which would check
if a string has only new line character in it. At the end I found that a
method "inspect" will do the trick.

Is that the only way to handle new line characters?

How about other special characters, if there is a need to search for
them in a text?

Code that didn't work:

if txtArray <=> "\n" then

Code that worked:

if txtArray.inspect <=> "\n" then

Thanks for all comments.
 
C

Chris Kottom

[Note: parts of this message were removed to make it a legal post.]

I assume that txtArray is a String in spite of the name. If so, then your
comparison is probably easiest done through regular expressions:

if txtArray =~ /\n/
...
end
 
D

Damir Sigur

Thanks, that worked.

I am on the way of learning more, and there is so much to learn!
 
7

7stud --

Actually, that solution does not work, as you can see here:

strings = ["hello\n", "\n"]

strings.each do |str|
if str =~ /\n/
puts "yes -- #{str}"
end
end

--output:--
yes -- hello
yes --


You can use == to test whether a string contains only a newline:

strings = ["hello\n", "\n"]

strings.each do |str|
if str == "\n"
puts "yes -->#{str}<--"
end
end


--output:--
yes -->
<--
 
7

7stud --

And if you want to replace all the newlines in some text, you can use
gsub():

str =<<ENDOFSTRING
hello world
goodbye
hi again world
ENDOFSTRING


result = str.gsub(/\n/, ' ')
puts result

--output:--
hello world goodbye hi again world
 
C

Chris Kottom

[Note: parts of this message were removed to make it a legal post.]

7stud is right. I missed the important word "only" in your original email.
My original method will produce false positives if you are indeed
interested in finding strings which are only "\n".

The "==" method works fine. Regexp is also possible (though probably
slower) if done as:

if str =~ /^\n$/
 
B

Brian Candler

Chris Kottom wrote in post #989369:
7stud is right. I missed the important word "only" in your original
email.
My original method will produce false positives if you are indeed
interested in finding strings which are only "\n".

The "==" method works fine. Regexp is also possible (though probably
slower) if done as:

if str =~ /^\n$/

Wrong - that doesn't match only strings containing just a newline.
=> 0

With a regexp, you need str =~ /\A\n\z/
=> nil

(beware: this is an area where Ruby's regexps are *not* the same as
Perl's regexps)
 
R

Robert Klemme

I am new to ruby, and was trying to make a small code which would check
if a string has only new line character in it. At the end I found that a
method "inspect" will do the trick.

Is that the only way to handle new line characters?

How about other special characters, if there is a need to search for
them in a text?

Code that didn't work:

if txtArray <=> "\n" then

Code that worked:

if txtArray.inspect <=> "\n" then

Thanks for all comments.


If you want to remove the trailing newline, this may help as well:

irb(main):005:0> s="foo\n"
=> "foo\n"
irb(main):006:0> s.chomp! and puts "there was a newline!"
there was a newline!
=> nil
irb(main):007:0> s
=> "foo"
irb(main):008:0> s.chomp! and puts "there was a newline!"
=> nil


For the "only newline" check you could do

irb(main):010:0> s="foo\n"
=> "foo\n"
irb(main):011:0> s.chomp! == ""
=> false
irb(main):012:0> s="\n"
=> "\n"
irb(main):013:0> s.chomp! == ""
=> true

Kind regards

robert
 
R

Robert Klemme

I am new to ruby, and was trying to make a small code which would check
if a string has only new line character in it. At the end I found that a
method "inspect" will do the trick.

Is that the only way to handle new line characters?

How about other special characters, if there is a need to search for
them in a text?

Code that didn't work:

if txtArray <=> "\n" then

Code that worked:

if txtArray.inspect <=> "\n" then

Thanks for all comments.


If you want to remove the trailing newline, this may help as well:

irb(main):005:0> s="foo\n"
=> "foo\n"
irb(main):006:0> s.chomp! and puts "there was a newline!"
there was a newline!
=> nil
irb(main):007:0> s
=> "foo"
irb(main):008:0> s.chomp! and puts "there was a newline!"
=> nil

Kind regards

robert
 
X

Xavier Noria

Damir, to know if a string is exactly a newline, please simply use ==:

if str == "\n"
...
end

There are indirect ways to get the same result, you can also go from
Barcelona to Paris via New York, but a straight line is nicer.

Please people stop suggesting regexps for this simple problem.
 
S

Stu

don't know if this will help you but ruby 1.9 has the each_line method.

example:

def line_count( s)
c = 0
s.each_line { |l| c+=1 }
return( c)
end
=> 4

if you want to test against the ascii equivalent in 1.8.7 it's ?\n and
in 1.9.2 it's "\n".ord which should return 10 in decimal format.


I am new to ruby, and was trying to make a small code which would check
if a string has only new line character in it. At the end I found that a
method "inspect" will do the trick.

Is that the only way to handle new line characters?

How about other special characters, if there is a need to search for
them in a text?

Code that didn't work:

if txtArray <=> "\n" then

Code that worked:

if txtArray.inspect <=> "\n" then

Thanks for all comments.
 

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
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top