Counting pairs of newline characters in a string

Discussion in 'Ruby' started by celldee, Apr 15, 2007.

  1. celldee

    celldee Guest

    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
     
    celldee, Apr 15, 2007
    #1
    1. Advertising

  2. celldee

    Chad Fowler Guest

    On 4/14/07, celldee <> 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
     
    Chad Fowler, Apr 15, 2007
    #2
    1. Advertising

  3. celldee

    celldee Guest

    Thanks Chad, works a treat.

    On Apr 15, 1:00 am, "Chad Fowler" <> wrote:
    > On 4/14/07, celldee <> 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
     
    celldee, Apr 15, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mobil
    Replies:
    3
    Views:
    254
    Basilisk96
    May 1, 2007
  2. Bill

    Counting characters in a string

    Bill, Aug 22, 2003, in forum: ASP General
    Replies:
    0
    Views:
    126
  3. PerlFAQ Server
    Replies:
    0
    Views:
    210
    PerlFAQ Server
    Jan 14, 2011
  4. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    213
    Dr John Stockton
    Sep 7, 2005
  5. majna
    Replies:
    4
    Views:
    703
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page