Counting pairs of newline characters in a string

C

celldee

I have a string that is text read in from a file. Within it there are
pairs of newline characters and I want to count how many there are.
Here is an irb example:-

irb(main):001:0> str = "bob
irb(main):002:0"
irb(main):003:0" and Jane"
=> "bob\n\nand Jane"
irb(main):004:0> str.count("\n\n")
=> 2

Each newline character is counted separately so I get the number 2
returned instead of the number 1 which is what I require.
Is there a way for me to count pairs of newline characters with a
simple function?

Regards,

Chris
http://rotsog.wordpress.com
 
C

Chad Fowler

I have a string that is text read in from a file. Within it there are
pairs of newline characters and I want to count how many there are.
Here is an irb example:-

irb(main):001:0> str = "bob
irb(main):002:0"
irb(main):003:0" and Jane"
=> "bob\n\nand Jane"
irb(main):004:0> str.count("\n\n")
=> 2

Each newline character is counted separately so I get the number 2
returned instead of the number 1 which is what I require.
Is there a way for me to count pairs of newline characters with a
simple function?

Hi. One way is: str.scan(/\n\n/).size

Chad
 

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,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top