• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

character count function

Explorer ,
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

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!
TOPICS
Advanced techniques

Views

440

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , May 11, 2007 May 11, 2007
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

Votes

Translate

Translate
LEGEND ,
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

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"))>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

You cannot use the ListLen( ) function without taking care of the consecutive occurrences of "|". The code below should give you what you want.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 11, 2007 May 11, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation