Words (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Template:String:Words subtitle}}
{{Template:String:Words subtitle}}
<var>Words</var> returns a substring of the method object string using word counts. The method returns a specified number of words, starting from a specified word and including consecutive following words.
<p></p>
 
Note that processing a string of words may be better accomplished with the <var>[[StringTokenizer class|StringTokenizer]]</var> class.
Note that processing a string of words may be better accomplished with the <var>[[StringTokenizer class|StringTokenizer]]</var> class.
   
   
==Syntax==
==Syntax==
{{Template:String:Words syntax}}
{{Template:String:Words syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%outString</th><td> <var>Words</var> returns the substring of the <var class="term">string</var> method object which starts at word number <var class="term">start</var> and continues for up to as many words as <var class="term">count</var>.
<tr><th nowrap>%outString</th><td>The returned substring of <var class="term">string</var>. <var class="term">%outString</var> contains as many words as <var class="term">count</var>, including and continuing from word number <var class="term">start</var>.  As shown in the [[#Whitespace not collapsed|"Whitespace not collapsed"]] example, whitespace characters within the return result are not collapsed.
</td></tr>
</td></tr>
<tr><th>string</th>
<tr><th>string</th>
<td>The method object string, from which the substring is extracted.
<td>The method object string, from which substring <var class="term">%outString</var> is extracted.  
</td></tr>
</td></tr>
<tr><th>start</th>
<tr><th>start</th>
<td>The number of the word in <var class="term">string</var> that is the start of the extracted substring; must be greater than or equal to 1 (1 designates the first word). If <var class="term">start</var> is greater than the number of words in <var class="term">string</var>, the null string is returned.
<td>The number of the word in <var class="term">string</var> that is the start of the extracted <var class="term">%outString</var>. <var class="term">start</var> must be greater than or equal to 1, which designates the first word. If <var class="term">start</var> is greater than the number of words in <var class="term">string</var>, the null string is returned. </td></tr>
</td></tr>
 
<tr><th>count</th>
<tr><th>count</th>
<td>The number of words in the substring to extract. This is an optional argument; the default is to extract the rest of <var class="term">string</var> starting at word number <var class="term">start</var>. If <var class="term">count</var> is greater than the number of words remaining in <var class="term">string</var> starting with word number <var class="term">start</var>, the remainder of the <var class="term">string</var> starting at word number <var class="term">start</var> is returned. This argument must be non-negative; if zero, the null string is returned.
<td>The number of words in <var class="term">string</var> to extract to <var class="term">%outString</var>. This is an optional argument; the default is to extract the rest of <var class="term">string</var> starting at word number <var class="term">start</var>. If <var class="term">count</var> is greater than the number of words remaining in <var class="term">string</var> starting with word number <var class="term">start</var>, the remainder of the <var class="term">string</var> starting at word number <var class="term">start</var> is returned.  
</td></tr>
 
This argument must be non-negative; if zero, the null string is returned. </td></tr>
 
<tr><th><var>Spaces</var></th>
<tr><th><var>Spaces</var></th>
<td>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.
<td>This optional, [[Notation conventions for methods#Named parameters|name required]], argument is 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. See the [[#Whitespace not collapsed|"Whitespace not collapsed"]] example. </td></tr>
</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<li><var>Words</var> is the OO version of <var>[[$Lstr_SubWord]]</var>.
</ul>
==Examples==
==Examples==
===Whitespace not collapsed===
The following fragment:
<p class="code">printText {~='a!b@@@c':words(2, spaces='@!')}
</p>
produces this result:
<p class="output">'a!b@@@c':words(2, spaces='@!')=b@@@c
</p>
<ul>
<li>The output result contains exactly the selected input substring; multiple whitespace characters are not collapsed.
</ul>
===Miscellaneous===
The following fragment:
The following fragment:
<p class="code">PrintText {~='aa bb cc':Words(1, 2)}.
<p class="code">printText {~='aa bb cc':words(2)}.
PrintText {~='aa bb cc':Words(4, 1)}.
printText {~='aa bb cc':words(1, 2)}.
PrintText {~='aa bb cc':Words(3, 2)}.
printText {~='aa bb cc':words(4, 1)}.
PrintText {~='aa bb cc':Words(2, 0)}.
printText {~='aa bb cc':words(3, 2)}.
printText {~='aa bb cc':words(2, 0)}.
</p>
</p>
produces the following result:
produces this result:
<p class="output">'aa bb cc':Words(2)=bb cc.
<p class="output">'aa bb cc':words(2)=bb cc.
'aa bb cc':Words(1, 2)=aa bb.
'aa bb cc':words(1, 2)=aa bb.
'aa bb cc':Words(4, 1)=.
'aa bb cc':words(4, 1)=.
'aa bb cc':Words(3, 2)=cc.
'aa bb cc':words(3, 2)=cc.
'aa bb cc':Words(2, 0)=.
'aa bb cc':words(2, 0)=.
</p>
</p>
==See also==
==See also==
<ul>
<ul>
<li>[[List of StringTokenizer methods]]
<li>[[List of StringTokenizer methods]]
<li><var>Words</var> is the OO version of <var>$Lstr_SubWord</var>.
<li><var>[[Word (String function)|Word]]</var>, which selects a word from a list of words
<li><var>[[WordCount (String function)|WordCount]]</var>, which counts the words in a list
<li><var class="camel">[[WordNumberIn and WordNumberOf (String functions)|WordNumberIn and WordNumberOf]]</var>, which return the word number of a "needle" word within a "haystack" list of words
</ul>
</ul>
{{Template:String:Words footer}}
{{Template:String:Words footer}}

Latest revision as of 22:51, 3 September 2015

Sublist of list of words (String class)

[Introduced in Sirius Mods 7.9]

Words returns a substring of the method object string using word counts. The method returns a specified number of words, starting from a specified word and including consecutive following words.

Note that processing a string of words may be better accomplished with the StringTokenizer class.

Syntax

%outString = string:Words( start, [count], [Spaces= string])

Syntax terms

%outStringThe returned substring of string. %outString contains as many words as count, including and continuing from word number start. As shown in the "Whitespace not collapsed" example, whitespace characters within the return result are not collapsed.
string The method object string, from which substring %outString is extracted.
start The number of the word in string that is the start of the extracted %outString. start must be greater than or equal to 1, which designates the first word. If start is greater than the number of words in string, the null string is returned.
count The number of words in string to extract to %outString. This is an optional argument; the default is to extract the rest of string starting at word number start. If count is greater than the number of words remaining in string starting with word number start, the remainder of the string starting at word number start is returned. This argument must be non-negative; if zero, the null string is returned.
Spaces This optional, name required, argument is 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. See the "Whitespace not collapsed" example.

Usage notes

Examples

Whitespace not collapsed

The following fragment:

printText {~='a!b@@@c':words(2, spaces='@!')}

produces this result:

'a!b@@@c':words(2, spaces='@!')=b@@@c

  • The output result contains exactly the selected input substring; multiple whitespace characters are not collapsed.

Miscellaneous

The following fragment:

printText {~='aa bb cc':words(2)}. printText {~='aa bb cc':words(1, 2)}. printText {~='aa bb cc':words(4, 1)}. printText {~='aa bb cc':words(3, 2)}. printText {~='aa bb cc':words(2, 0)}.

produces this result:

'aa bb cc':words(2)=bb cc. 'aa bb cc':words(1, 2)=aa bb. 'aa bb cc':words(4, 1)=. 'aa bb cc':words(3, 2)=cc. 'aa bb cc':words(2, 0)=.

See also