Loading ...
Sorry, an error occurred while loading the content.

count the occurrence of a substring in a string

Expand Messages
  • rizwana_009
    Hi all, Hope you all are well and doing good. I am stuck in one problem, hope anyone has the solution of it. I am taking a text from a form. I need to check
    Message 1 of 3 , Dec 3, 2008
    View Source
    • 0 Attachment
      Hi all,
      Hope you all are well and doing good. I am stuck in one problem, hope
      anyone has
      the solution of it.

      I am taking a text from a form. I need to check the no. of occurrences
      of a single word say "hello" in that text and replace it with its
      occurrence number. For example in a text the word "hello" occurs 5
      times. So i need to replace the first "hello" with "1" and the second
      "hello" with "2" and so on till the last word.

      Waiting for your Responses,
    • मयंक जैन (makuchaku)
      Hi, Use n=String.index(token) and loop through the string with a counter. With every success find, replace the token in the string (at n th position) with the
      Message 2 of 3 , Dec 6, 2008
      View Source
      • 0 Attachment
        Hi,

        Use n=String.index(token) and loop through the string with a counter. With
        every success find, replace the token in the string (at n'th position) with
        the counter.

        I hope it helps...

        --
        Maku



        On Thu, Dec 4, 2008 at 12:54 PM, rizwana_009 <rizwana_009@...>wrote:

        > Hi all,
        > Hope you all are well and doing good. I am stuck in one problem, hope
        > anyone has
        > the solution of it.
        >
        > I am taking a text from a form. I need to check the no. of occurrences
        > of a single word say "hello" in that text and replace it with its
        > occurrence number. For example in a text the word "hello" occurs 5
        > times. So i need to replace the first "hello" with "1" and the second
        > "hello" with "2" and so on till the last word.
        >
        > Waiting for your Responses,
        >
        >
        >


        [Non-text portions of this message have been removed]
      • C. K. Ponnappa
        [Cross posting another solution the same question from the BRUG list] that s with the missing initializer of course counter=0 form_text_string.gsub!(/hello/)
        Message 3 of 3 , Dec 7, 2008
        View Source
        • 0 Attachment
          [Cross posting another solution the same question from the BRUG list]

          that's with the missing initializer of course

          counter=0
          form_text_string.gsub!(/hello/) {counter+=1}



          On 4 Dec, 17:55, mataal <mat...@...> wrote:

          > > how about this?
          > >
          > > form_text_string.gsub!(/hello/) {counter+=1}
          > >
          > > On 4 Dec, 12:25, rizwana <rizzwanab...@...> wrote:
          > >
          >
          >> > > Hi all,
          >> > > Hope you all are well and doing good. I am stuck in one problem, hope
          >> > > anyone has
          >> > > the solution of it.
          >>
          > >
          >
          >> > > I am taking a text from a form. I need to check the no. of occurrences
          >> > > of a single word say "hello" in that text and replace it with its
          >> > > occurrence number. For example in a text the word "hello" occurs 5
          >> > > times. So i need to replace the first "hello" with "1" and the second
          >> > > "hello" with "2" and so on till the last word.
          >>
          > >
          >
          >> > > Waiting for your Responses,
          >>

          मयंक जैन (makuchaku) wrote:
          >
          > Hi,
          >
          > Use n=String.index(token) and loop through the string with a counter. With
          > every success find, replace the token in the string (at n'th position)
          > with
          > the counter.
          >
          > I hope it helps...
          >
          > --
          > Maku
          >
          > On Thu, Dec 4, 2008 at 12:54 PM, rizwana_009 <rizwana_009@...
          > <mailto:rizwana_009%40yahoo.co.in>>wrote:
          >
          > > Hi all,
          > > Hope you all are well and doing good. I am stuck in one problem, hope
          > > anyone has
          > > the solution of it.
          > >
          > > I am taking a text from a form. I need to check the no. of occurrences
          > > of a single word say "hello" in that text and replace it with its
          > > occurrence number. For example in a text the word "hello" occurs 5
          > > times. So i need to replace the first "hello" with "1" and the second
          > > "hello" with "2" and so on till the last word.
          > >
          > > Waiting for your Responses,
          > >
          > >
          > >
          >
          > [Non-text portions of this message have been removed]
          >
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.