StringUpTo (StringTokenizer function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:StringTokenizer:StringUpTo subtitle}}
{{Template:StringTokenizer:StringUpTo subtitle}}
 
This method returns a substring of the tokenizing string, starting at the current position and ending just before the occurrence of its argument string.  
This method returns a substring of the tokenizing string, starting at <var>[[NextPosition (StringTokenizer property)|NextPosition]]</var> and ending just before the occurrence of its argument string, and <var>NextPosition</var> is set to the start of the occurrence of the argument string.
==Syntax==
==Syntax==
{{Template:StringTokenizer:StringUpTo syntax}}
{{Template:StringTokenizer:StringUpTo syntax}}
 
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%string</th>
<tr><th>%string</th>
<td>A string variable to contain the returned substring.</td></tr>
<td>A string variable to contain the returned substring, which starts at <var>NextPosition</var> and continues up to the occurrence of the needle <var class="term">string</var>, or, if the needle is not found or is the null string, continues through the rest of the string being tokenized.</td></tr>
 
<tr><th>stringTokenizer</th>
<tr><th>stringTokenizer</th>
<td>A <var>StringTokenizer</var> object expression.</td></tr>
<td>A <var>StringTokenizer</var> object expression.</td></tr>
 
<tr><th>string</th>
<tr><th>string</th>
<td>The string within <var class="term">stringTokenizer</var> up to which the tokenizer scans to determine the substring to return to <var class="term">%string</var>.  
<td>The needle to search for, starting at <var>NextPosition</var>.
<p>If <var class="term">string</var> is not found, <var>StringUpTo</var> returns the rest of the string being    
<p>If <var class="term">string</var> is not found, or if it is the null string,
tokenized. </p></td></tr>
<var>StringUpTo</var> returns the rest of the string being
tokenized, and sets <var>AtEnd</var> to <var>True</var> and <var>NextPosition</var> to one more than the
length of the string being tokenized.
</p></td></tr>
</table>
</table>
 
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If <var>StringUpTo</var> locates an occurrence of its argument string, it advances the tokenizing position to the end of that located argument string.
<li>If <var>StringUpTo</var> locates an occurrence of its argument string, it advances <var>NextToken</var> to the start of that located argument string.
 
<li>Because a failure to match the target string returns the rest of the tokenizer string, <var>StringUpTo</var> does not directly indicate whether there was a match.  Either of the following two techniques indicate there was a match:
<li>Because a failure to match the target string returns the rest of the tokenizer string, <var>StringUpTo</var> leaves no easy way to determine if you get a result string because you got a match or because you went to the end of the string. A good technique then may be to follow a <var>StringUpTo</var> call with a <var>[[NextPosition (StringTokenizer function)|NextPosition]]</var> call to check your result.
<ul>
<li><var>AtEnd</var> is <var>False</var> if there was a match, and <var>True</var> if there was not a match.
<li><var>NextPosition</var> is less than or equal to <var>StringLength</var> if there was a match, and greater than
<var>StringLength</var> if there was not a match.
</ul>
</ul>
 
==Examples==
==Examples==
===Spaces tokenization===
Here are two simple <var>StringUpTo</var> calls, the second of which does
Here are two simple <var>StringUpTo</var> calls, the second of which does
not locate its argument string:
not locate its argument string:
<p class="code">b                                
<p class="code">b
%tk is object stringTokenizer  
%tk is object stringTokenizer
%ls is longstring
%tk = new
%tk = new  
%tk:string = 'a b c --> d e'
%tk:string = 'a b c --> d e'  
 
printText {~} = /{%tk:stringUpTo('-->')}/
%ls = %tk:stringUpTo('-->')  
[[Targeted Text statements#AuditText, PrintText, and TraceText|PrintText]] {~} = {%ls}            
printtext {~} = {%tk:nextPosition}
printtext {~} = {%tk:nextPosition}
 
%ls = %tk:stringUpTo('---')  
printText {~} = /{%tk:stringUpTo('---')}/
printtext {~} = {%ls}            
printtext {~} = {%tk:nextPosition}
printtext {~} = {%tk:nextPosition}
end </p>
end </p>
 
The result is:
The result is:
<p class="output">
<p class="output">%tk:stringUpTo('-->') = /a b c /
%ls = a b c        
%tk:nextPosition = 7
%tk:nextPosition = 7  
%tk:stringUpTo('---') = /--> d e/
%ls = --> d e      
%tk:nextPosition = 14, %tk:atEnd = True
%tk:nextPosition = 14 </p>
</p>
 
===Separators tokenization===
Similarly, in the following fragment, the second <var>StringUpTo</var> call does not
locate its argument string.  Note that <var>NextPosition</var> is the same
before and after the second <var>StringUpTo</var> call, but <var>AtEnd</var>
changes:
<p class="code">%tk:separators = ','
%tk:string = 'a b c --> d e,q'
printtext {~} = {%tk:nextToken}
printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd}
printText {~} = /{%tk:stringUpTo('?')}/
printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd}
%tk:string = 'a b c --> d e,'
printtext {~} = {%tk:nextToken}
printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd}
printText {~} = /{%tk:stringUpTo('?')}/
printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd}
</p>
<p class="output">%tk:nextToken = a b c --> d e
%tk:nextPosition = 15, %tk:stringLength = 15, %tk:atEnd = False
%tk:stringUpTo('?') = /q/
%tk:nextPosition = 16, %tk:stringLength = 15, %tk:atEnd = True
%tk:nextToken = a b c --> d e
%tk:nextPosition = 15, %tk:stringLength = 14, %tk:atEnd = False
%tk:stringUpTo('?') = //
%tk:nextPosition = 15, %tk:stringLength = 14, %tk:atEnd = True
</p>
==See also==
==See also==
{{Template:StringTokenizer:StringUpTo footer}}
{{Template:StringTokenizer:StringUpTo footer}}

Latest revision as of 20:57, 16 July 2019

String from start to current position (StringTokenizer class)

[Introduced in Sirius Mods 7.8]


This method returns a substring of the tokenizing string, starting at NextPosition and ending just before the occurrence of its argument string, and NextPosition is set to the start of the occurrence of the argument string.

Syntax

%string = stringTokenizer:StringUpTo( string)

Syntax terms

%string A string variable to contain the returned substring, which starts at NextPosition and continues up to the occurrence of the needle string, or, if the needle is not found or is the null string, continues through the rest of the string being tokenized.
stringTokenizer A StringTokenizer object expression.
string The needle to search for, starting at NextPosition.

If string is not found, or if it is the null string, StringUpTo returns the rest of the string being tokenized, and sets AtEnd to True and NextPosition to one more than the length of the string being tokenized.

Usage notes

  • If StringUpTo locates an occurrence of its argument string, it advances NextToken to the start of that located argument string.
  • Because a failure to match the target string returns the rest of the tokenizer string, StringUpTo does not directly indicate whether there was a match. Either of the following two techniques indicate there was a match:
    • AtEnd is False if there was a match, and True if there was not a match.
    • NextPosition is less than or equal to StringLength if there was a match, and greater than StringLength if there was not a match.

    Examples

    Spaces tokenization

    Here are two simple StringUpTo calls, the second of which does not locate its argument string:

    b %tk is object stringTokenizer %tk = new %tk:string = 'a b c --> d e' printText {~} = /{%tk:stringUpTo('-->')}/ printtext {~} = {%tk:nextPosition} printText {~} = /{%tk:stringUpTo('---')}/ printtext {~} = {%tk:nextPosition} end

    The result is:

    %tk:stringUpTo('-->') = /a b c / %tk:nextPosition = 7 %tk:stringUpTo('---') = /--> d e/ %tk:nextPosition = 14, %tk:atEnd = True

    Separators tokenization

    Similarly, in the following fragment, the second StringUpTo call does not locate its argument string. Note that NextPosition is the same before and after the second StringUpTo call, but AtEnd changes:

    %tk:separators = ',' %tk:string = 'a b c --> d e,q' printtext {~} = {%tk:nextToken} printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd} printText {~} = /{%tk:stringUpTo('?')}/ printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd} %tk:string = 'a b c --> d e,' printtext {~} = {%tk:nextToken} printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd} printText {~} = /{%tk:stringUpTo('?')}/ printtext {~} = {%tk:nextPosition}, {~} = {%tk:stringLength}, {~} = {%tk:atEnd}

    %tk:nextToken = a b c --> d e %tk:nextPosition = 15, %tk:stringLength = 15, %tk:atEnd = False %tk:stringUpTo('?') = /q/ %tk:nextPosition = 16, %tk:stringLength = 15, %tk:atEnd = True %tk:nextToken = a b c --> d e %tk:nextPosition = 15, %tk:stringLength = 14, %tk:atEnd = False %tk:stringUpTo('?') = // %tk:nextPosition = 15, %tk:stringLength = 14, %tk:atEnd = True

    See also