Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Counting pairs of newline characters in a string

Reply
Thread Tools

Counting pairs of newline characters in a string

 
 
celldee
Guest
Posts: n/a
 
      04-14-2007
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


 
Reply With Quote
 
 
 
 
Chad Fowler
Guest
Posts: n/a
 
      04-15-2007
On 4/14/07, celldee <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
celldee
Guest
Posts: n/a
 
      04-15-2007
Thanks Chad, works a treat.

On Apr 15, 1:00 am, "Chad Fowler" <(E-Mail Removed)> wrote:
> On 4/14/07, celldee <(E-Mail Removed)> wrote:
>
>
>
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting utf-8 characters -special characters majna Javascript 4 09-19-2007 01:53 PM
Removing the continous newline characters from the pythong string mobil Python 3 05-01-2007 10:09 PM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting occurances of string A in string B, and adding it to string B Sandman Perl Misc 7 08-03-2004 08:46 PM
Counting characters in a string Bill ASP General 0 08-22-2003 05:16 PM



Advertisments