5 Replies Latest reply on May 11, 2007 10:55 AM by Newsgroup_User

    character count function

    HandersonVA Level 1
      can anyone help me find how many "|" in the string? does coldfusion has the function for it?
      <cfset variables.teststring = "343|4w3|||jenny||sfsdfsd">
      thank you!
        • 1. Re: character count function
          Level 7
          HandersonVA wrote:
          > can anyone help me find how many "|" in the string? does coldfusion has the function for it?
          > <cfset variables.teststring = "343|4w3|||jenny||sfsdfsd">
          > thank you!

          Not built in, but this popped into my head.

          <cfset variables.pipes = len(rereplace(variables.teststring,"^|","","ALL"))>
          • 2. character count function
            jdeline Level 1
            You cannot use the ListLen( ) function without taking care of the consecutive occurrences of "|". The code below should give you what you want.
            • 3. character count function
              paross1 Level 2
              Problem... you are counting one too many "pipes"... there are actually only 6 | characters, but your example gives a count of 7, since you are actually counting list elements with the | being the delimiter, and there is actually one more element than delimiter. Plus, you get into trouble if the | is the first or last character in the string, etc.

              Phil
              • 4. character count function
                paross1 Level 2
                Ian, you did a slight oops, but otherwise I think that it is a good solution, since ^| only means NOT | if it is enclosed in [ ] brackets. Without them ^ means start of line.

                Phil
                • 5. Re: character count function
                  Level 7
                  paross1 wrote:
                  > Ian, you did a slight oops, but otherwise I think that it is a good
                  > solution....
                  >
                  > <cfset variables.pipes = len(rereplace(variables.teststring,"^|","","ALL"))>
                  > should actually be
                  > <cfset variables.pipes = len(rereplace(variables.teststring,"[^|]","","ALL"))>
                  >
                  > Since ^| only means NOT | if it is enclosed itn [ ] brackets.
                  >
                  > Phil
                  >
                  That's what I get for typing my code into an e-mail editor. The
                  'debugger' sucks! :-)

                  Thanks for the correction.