PositionIn and PositionOf (String functions): Difference between revisions
m (match syntax diagram to revised template; fix tags.) |
m (Full stop) |
||
Line 51: | Line 51: | ||
==See also== | ==See also== | ||
<ul><li>For details of the <var>printtext</var> statement, please see <var>[[Intrinsic classes#printtext|printText]] | <ul><li>For details of the <var>printtext</var> statement, please see <var>[[Intrinsic classes#printtext|printText]]</var>.</ul> | ||
{{Template:String:PositionIn and PositionOf footer}} | {{Template:String:PositionIn and PositionOf footer}} |
Revision as of 23:25, 3 February 2011
The position of one string inside another (String class)
The PositionIn and PositionOf functions return the numeric position of the first occurrence of one string inside another. The difference between the two methods is that for PositionIn, the method object string is located in the first argument string, whereas for PositionOf, the first argument string is located in the method object string. Which is more convenient to use will be application dependent.
Syntax
%position = string:PositionIn( haystack, [Start= number])
%position = string:PositionOf( needle, [Start= number])
Syntax terms
%position | A variable to receive the position of the first occurrence of the needle string in the haystack string, starting at the implicit or explicit starting position. If needle is not found in haystack, %position is set to 0. |
---|---|
string | The method object string. Depending upon whether the PositionIn or PositionOf method is being used, string may be either the:
|
needle | The search target string to be located in the search space haystack string. |
haystack | The search space string in which the search target needle is to be located. |
Start | This is an optional, but nameRequired, parameter which is a number specifying the position in the search space haystack string at which to start searching for the search target needle string. Start defaults to 1, meaning that the search begins at the first character in haystack. |
Usage notes
- The start position must be a positive number. A zero or negative number results in request cancellation. Specifying a start position greater than the length of (haystack plus one minus the length of needle) returns a zero, because there are not enough characters in haystack to satisfy the search.
- PositionIn / PositionOf do exactly the same thing. The only difference between them is that in PositionOf, the haystack is the method object string and the needle is the first argument. In PositionIn, the method object string and first argument are reversed. Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
- PositionIn / PositionOf are the object-oriented equivalents of the User Language $index function.
- PositionIn / PositionOf are available as of Sirius Mods Version 7.2.
Examples
- If
%s
contains"This is a test"
, the following statementsprintText {%s:positionOf('is')} printText {%s:positionOf('is', start=4)} printText {%s:positionOf('is', start=7)}
will display
3 6 0
- If
%s
contains"ver"
, the following statementsprintText {%s:positionIn('Never say never')} printText {%s:positionIn('Never say never', start=4)} printText {%s:positionIn('Never say never', start=14)}
will display
3 13 0
See also
- For details of the printtext statement, please see printText.