• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: counting w*rds
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: counting w*rds


  • Subject: Re: counting w*rds
  • From: Nigel Garvey <email@hidden>
  • Date: Wed, 23 May 2001 11:00:46 +0100

Ehsan Saffari wrote on Mon, 21 May 2001 23:03:20 -0600:

>There are many characters that AS considers a word:
>
>set x to "****"
>count words of x -->4
>
>how can I count the words in a string while
>ignoring a special character like "*" using vanilla AS:
>
>set x to "****"
>count words of x -->me needs 1

One fast way, if it's just asterisks that bother you and your string
isn't several chapters long, would be test instead a version of the
string whose asterisks have been changed to letters:

on countWords(str)
set AppleScript's text item delimiters to {"*"}
set theItems to the text items of str
set AppleScript's text item delimiters to {"a"}
set drStr to theItems as string
set AppleScript's text item delimiters to {""}
count words of drStr
end countWords

countWords("This line contains **** and *****.")

>and on a similar note, how to count the occurances of a substring in a
>string?

Text item delimiters can be used here too:

on countOccurrences of subStr into str
set AppleScript's text item delimiters to {subStr}
set theCount to (count text items of str) - 1
set AppleScript's text item delimiters to {""}
return theCount
end countOccurrences

But you'll need something a bit more complex for overlapping substrings,
such as the instances of "ana" in "banana".

NG


  • Follow-Ups:
    • Pressing Filemaker's Continue Button
      • From: Gary Beberman <email@hidden>
  • Prev by Date: Re: Scope of globals, parents and children
  • Next by Date: OSX and Applet permissions
  • Previous by thread: Re: counting w*rds
  • Next by thread: Pressing Filemaker's Continue Button
  • Index(es):
    • Date
    • Thread