Difference between revisions of "StartsWith and IsStartOf (String functions)"

From m204wiki
Jump to navigation Jump to search
Line 17: Line 17:
 
==Examples==
 
==Examples==
 
===Determining if option is valid abbreviation===
 
===Determining if option is valid abbreviation===
One common task is confirming whether some user input is a valid abbreviation for one of several longer terms.  This is one motivation for the <var>MinLength</var> argument; there may be overlap in the initial substrings of the longer terms.  Consider the following fragment:
+
A common task is checking user input for a valid abbreviation for one of several longer terms.  This is one motivation for the <var>MinLength</var> argument; there may be overlap in the initial substrings of the longer terms.  Consider the following fragment:
 
<p class="code">%opt = %(system):[[Arguments (System function)|arguments]]:toUpper
 
<p class="code">%opt = %(system):[[Arguments (System function)|arguments]]:toUpper
 
if    %opt:isStartOf('MAXAMOUNT', 5) then
 
if    %opt:isStartOf('MAXAMOUNT', 5) then
Line 26: Line 26:
 
</p>
 
</p>
 
If the argument to this proc is <code>max</code>, that would not distinguish it between <code>maxAmount</code> and <code>maxTime</code>; hence the <var>MinLength</var> argument value of 5 is used to enforce that at least <code>maxAm</code> or <code>maxTi</code> is entered (a value of 4 would also have disambiguated the abbreviations in this case).
 
If the argument to this proc is <code>max</code>, that would not distinguish it between <code>maxAmount</code> and <code>maxTime</code>; hence the <var>MinLength</var> argument value of 5 is used to enforce that at least <code>maxAm</code> or <code>maxTi</code> is entered (a value of 4 would also have disambiguated the abbreviations in this case).
 +
 
===Several single-line examples===
 
===Several single-line examples===
 
The following fragment:
 
The following fragment:

Revision as of 02:51, 24 May 2011

Is the method object string an initial substring of the argument string? (String class) [introduced in Sirius Mods 7.9 βeta]


Syntax

%boolean = string:IsStartOf( longerString, MinLength= number)

Syntax terms

%boolean The Boolean result of IsStartOf is True if:
  • The length of the method object string is at least the value of the MinLength argument
    and:
  • the method object string is the same as the initial substring of the MinLength argument, whose length is the length of string.
If either of the above conditions does not hold, the result is False.
string A string which is examined to see if it is an abbreviation (i.e., an initial substring) of longerString.
longerString A string which is examined to see if the method object string is an abbreviation of it.
MinLength A non-negative number, specifying the minimum length of the method object string to be considered an abbreviation of longerString.

Examples

Determining if option is valid abbreviation

A common task is checking user input for a valid abbreviation for one of several longer terms. This is one motivation for the MinLength argument; there may be overlap in the initial substrings of the longer terms. Consider the following fragment:

%opt = %(system):arguments:toUpper if %opt:isStartOf('MAXAMOUNT', 5) then ... elseIf %opt:isStartOf('MAXTIME', 5) then ... else PrintText Invalid argument: {%opt}

If the argument to this proc is max, that would not distinguish it between maxAmount and maxTime; hence the MinLength argument value of 5 is used to enforce that at least maxAm or maxTi is entered (a value of 4 would also have disambiguated the abbreviations in this case).

Several single-line examples

The following fragment:

PrintText {~= 'a':IsStartOf('abc', 1) } PrintText {~= 'a':IsStartOf('abc', 2) } PrintText {~= 'a':IsStartOf('abc', 0) } PrintText {~= 'x':IsStartOf('abc', 1) } PrintText {~= 'x':IsStartOf('abc', 0) } PrintText {~= :IsStartOf('abc', 0) }

produces the following output:

'a':IsStartOf('abc', 1) = True 'a':IsStartOf('abc', 2) = False 'a':IsStartOf('abc', 0) = True 'x':IsStartOf('abc', 1) = False 'x':IsStartOf('abc', 0) = False

See also