Ask Ben: Counting Spaces In A Given String

Posted August 23, 2006 at 5:57 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

How can I get the number of spaces in a string?

This seemingly simple problem does not have the most simple answer. I wish there was some sort of ValueCount() method in ColdFusion, but right, I think that only applies to List (ie. ListValueCount()). Luckily for your particular problem, there is a mostly simple solution. Since you are looking for just spaces, we can strip out everything that is NOT a space and then just get the length of the resultant string:

  • <cfset intLength = Len(
  • REReplace(
  • "You are simply a vision in that dress!",
  • "[^ ]+",
  • "",
  • "ALL"
  • )
  • ) />

This really only works when you are looking for single characters. If you want to search for all instances of a word, then things get a bit hairy. The easy solution is simple to keep searching the string untill you cannot find any instances.

  • <!--- The test value. --->
  • <cfset strTest = "You are the best and the most beautiful girl." />
  •  
  • <!--- The target instance. --->
  • <cfset strTarget = "the" />
  •  
  • <!--- The instance counter. --->
  • <cfset intCount = 0 />
  •  
  • <!--- Get the initial position. --->
  • <cfset intPosition = Find( strTarget, strTest, 0 ) />
  •  
  • <!--- Keep searching till no more instances are found. --->
  • <cfloop condition="intPosition">
  •  
  • <!--- Increment instance counter. --->
  • <cfset intCount = (intCount + 1)>
  •  
  • <!--- Get the next position. --->
  • <cfset intPosition = Find(
  • strTarget,
  • strTest,
  • (intPosition + Len( strTarget ))
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- Output the number of target instances. --->
  • #intCount#

Each time we do a search, we have to increment the counter and then start the search again after the given instance. Not the greatest solution, but it works.



Reader Comments

Aug 23, 2006 at 11:10 PM // reply »
153 Comments

Why not leverage Java?

intCount=ArrayLen(strTest.split(strTarget.replaceAll("\W","\$1")))


Aug 23, 2006 at 11:12 PM // reply »
153 Comments

Erm, make that:

intCount=DecrementValue(ArrayLen(strTest.split(strTarget.replaceAll("\W","\$1"))))

Silly off-by-one error.


Aug 23, 2006 at 11:37 PM // reply »
153 Comments

Okay, last try, I promise.

<cfset strTest = "You are \the\ best (and) the [most] beautiful girl.">
<cfset strTarget = "\">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>


Aug 24, 2006 at 4:30 AM // reply »
5 Comments

Here's my simple take on it:

<cfset theString = "You are simply a vision in that dress!">
<cfset count = ListLen(theString," ") - 1>


Aug 24, 2006 at 5:13 AM // reply »
5 Comments

This could be done for phrases as well:

<cfset theString = replace("Today the times are changing, the weather is changing and there is something in the air"," the ","|","all")>
<cfset count = ListLen(theString,"|") - 1>

Obviously it would return wrong results if the phrase is at the begining or the endt of the string. This can easily be fixed by prepending and appending the string with some rubbish phrases.

ps - Ben those spam fighthing math equations are hard on me early in the morning ;)


Aug 24, 2006 at 1:07 PM // reply »
74 Comments

Rick, Trond,

Excellent suggestions all around. As we can see, there are a number of solutions to this problem, but still, I think this would be an easy method for CF to build in, right?

Trond, good call with the replacing the phrase with the "delimiter". That never even occurred to me. The only red flag I could see is that you might use a delimiter character that is already in the string (and therefore would throw off the count). This of course can be offset by using extrememly rare characters or even by replacing that character out before replacing out the target phrase.

Good stuff all around. Also sorry about the math, but it keeps the SPAM out :)


Feb 2, 2007 at 2:08 PM // reply »
168 Comments

We can take this further...

<cfset intLen = listLen(reReplaceNoCase(strTarget, "(?:(?!test)[\S\s])+", ",", "ALL")) />
Test, tester, and retest count as one match each, testtest counts as two matches.

<cfset intLen = listLen(reReplaceNoCase(strTarget, "(?:(?!\btest\b)[\S\s])+", ",", "ALL")) />
Test counts as one match, tester, retest, and testtest do not count as matches.

<cfset intLen = listLen(reReplaceNoCase(strTarget, "\b(?:(?!test)[\S\s])+\b", ",", "ALL")) />
Test, tester, retest, and testtest count as one match each.

Or, using my reMatch() UDF (http://badassery.blogspot.com/2007/01/coldfusion-regex-support-udfs-rematch.html), the regexes become even simpler...

<cfset intLen = arrayLen(reMatchNoCase("test", strTarget, 1, "ALL")) />
Test, tester, and retest count as one match each, testtest counts as two matches.

<cfset intLen = arrayLen(reMatchNoCase("\btest\b", strTarget, 1, "ALL")) />
Test counts as one match, tester, retest, and testtest do not count as matches.

<cfset intLen = arrayLen(reMatchNoCase("\b\w*?test\w*\b", strTarget, 1, "ALL")) />
Test, tester, retest, and testtest count as one match each.


Feb 2, 2007 at 2:16 PM // reply »
168 Comments

Note that I'm not familiar with using the underlying Java regex methods such as split(). I'm sure that at least my first three, non-reMatch()-based examples could be written more elegantly using the Java core. Goddamn CF7's lame regex support and available functions...


Feb 2, 2007 at 2:27 PM // reply »
10,640 Comments

Yeah, Java's regex stuff is really cool and very powerful. It can handle most of the regular expression stuff that straight-up CFMX method calls cannot handle. I use them all the time. I find that they are also a good bit faster.


Feb 2, 2011 at 9:24 PM // reply »
1 Comments

Thanks Ben! I used this to find the first space after the midway point in a document, so that I could split it into near length columns. I seem to end up are your blog posts more often than Adobe LiveDocs...

-Kyle


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »