Re: counting w*rds
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