StartsWith and IsStartOf (String functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
Line 51: Line 51:
PrintText {~= 'x':IsStartOf('abc', MinLength=1) }
PrintText {~= 'x':IsStartOf('abc', MinLength=1) }
PrintText {~= 'x':IsStartOf('abc', MinLength=0) }
PrintText {~= 'x':IsStartOf('abc', MinLength=0) }
PrintText {~= '':IsStartOf('abc', MinLength=0) }
PrintText {~= '':IsStartOf('abc', MinLength=0) }
</p>
</p>
produces the following output:
produces the following output:

Revision as of 18:42, 8 February 2012

Is one string an initial substring of a longer string? (String class)

[Introduced in Sirius Mods 7.9]


Syntax

%boolean = string:StartsWith( abbreviation)

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

StartsWith can be conveniently used to check whether a starts with a known string; IsStartOf can be conveniently used to check whether a string is an abbreviation of a known string.

Syntax terms

%boolean The Boolean result is True if abbreviation is the same as the initial substring, whose length is the length of abbreviation, of longerString.

In addition, for IsStartOf, the length of the method object abbreviation is at least the value of the MinLength argument.

string The method object string. Depending upon whether the StartsWith or IsStartOf method is being used, string may be either the:
  • longerString, whose intial characters are examined, for StartsWith
  • abbreviation, for IsStartOf
abbreviationA 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 abbreviation is an abbreviation of it.
MinLength This name-required argument is a non-negative number, specifying the minimum length of abbreviation to be considered an abbreviation of longerString. Note that if this is 0, then the null string is an abbreviation of any value of the longerString argument; if this is non-0, then the null string is never an abbreviation.

This argument is only used by IsStartOf; for StartsWith, MinLength is effectively 0.

Examples

Checking whether string starts with some literal

Hopefully the following fragment is self-evident:

if %name:toUpper:startsWith('DAV') then print 'Name is probably Dave or David'

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', MinLength=5) then ... elseIf %opt:isStartOf('MAXTIME', MinLenth=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', MinLength=1) } PrintText {~= 'a':IsStartOf('abc', MinLength=2) } PrintText {~= 'a':IsStartOf('abc', MinLength=0) } PrintText {~= 'x':IsStartOf('abc', MinLength=1) } PrintText {~= 'x':IsStartOf('abc', MinLength=0) } PrintText {~= '':IsStartOf('abc', MinLength=0) }

produces the following output:

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

See also