WordNumberIn and WordNumberOf (String functions)
The word number of a word within a list of words (String class)
[Introduced in Sirius Mods 7.9]
The WordNumberIn and WordNumberOf functions return the word number of the first occurrence of one word (the "needle") within a list of words ("the haystack"). The search respects character case. The difference between the two methods is that for WordNumberIn, the method object string is the "needle" and the first argument is the "haystack", whereas for WordNumberOf, the first argument is the "needle" and the method object string is the "haystack". Which method is more convenient to use will be application dependent.
Note that processing a string of words may be better accomplished with the StringTokenizer class.
Syntax
%number = string:WordNumberIn( haystack, [Spaces= string])
%number = string:WordNumberOf( needle, [Spaces= string])
Syntax terms
%number | A variable to receive the number of the first occurrence of the "needle" word in the "haystack" list of words. If the word "needle" is not one of the words in "haystack", %number is set to 0. |
---|---|
string | The method object string; either:
|
haystack | In WordNumberIn, the list of words within which the string method object (the "needle") is searched for. |
needle | In WordNumberOf, the word to be searched for within the string method object list of words (the "haystack"). |
Spaces | A set of "whitespace" characters, that is, characters that separate words. The default is the blank character. If the null string is provided, the blank character is used as the whitespace character. |
Usage notes
- WordNumberIn and WordNumberOf do exactly the same thing. The only difference between them is that in WordNumberOf, the "haystack" is the method object string and the needle is the first argument. In WordNumberIn, these roles are reversed: the "needle" is the method object string and the haystack is the first argument. Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
Examples
- The following fragment:
PrintText {~='aa bb cc':WordNumberOf('bb')} PrintText {~='bb':WordNumberIn('aa bb cc')}
produces the following result:
'aa bb cc':WordNumberOf('bb')=2 'bb':WordNumberIn('aa bb cc')=2
- You can use WordNumberIn and Word as
"quick and dirty" NamedArraylists:
%x = 'PA' %w = %x:WordNumberIn - ('MA PA MN') %s = ('Massachusetts Pennsylvania Minnesota'):Word(%w) Print 'Full state name:' And %s
However, as the number of words grows large, repeated use of WordNumberIn is less efficient than using a NamedArrayList.
See also
- List of StringTokenizer methods
- Word - Select word from list of words
- Words - Sublist of list of words
- WordCount - Count words in list
- WordNumberIn is the OO version of $Lstr_Windex.