UnicodePositionIn and UnicodePositionOf (Unicode functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (syntax terms, tags and links)
Line 1: Line 1:
{{Template:Unicode:UnicodePositionIn and UnicodePositionOf subtitle}}
{{Template:Unicode:UnicodePositionIn and UnicodePositionOf subtitle}}


These functions return the numeric position of the first occurrence of one
These <var>UnicodePositionIn</var> and <var>UnicodePositionOf</var> functions return the numeric position of the first occurrence of one character string inside another (character case respected). The difference between the two methods is that for <var>UnicodePositionIn</var>, the method object string is located in the first argument string, whereas for <var>UnicodePositionOf</var>, the first argument string is located in the method object string. Which method is more convenient to use will be application dependent.
character string inside another (character case respected).
The difference between the two methods is that for UnicodePositionIn, the method
object string is located in the first argument string, whereas for
UnicodePositionOf, the first argument string is located in the method object
string.
Which method is more convenient to use will be application dependent.


The UnicodePositionIn and UnicodePositionOf functions are
available as of version 7.5  of the <var class=product>Sirius Mods</var>.
==Syntax==
==Syntax==
{{Template:Unicode:UnicodePositionIn syntax}}
{{Template:Unicode:UnicodePositionIn syntax}}
Line 16: Line 8:
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%pos </th>
<tr><th>%number</th>
<td>A numeric variable to receive the position of the first occurrence of the ''needle'' character string in the ''haystack'' string, starting at the implicit or explicit starting position. If ''needle'' is not found in ''haystack'', ''spos'' is set to 0. </td></tr>
<td>A numeric variable to receive the position of the first occurrence of the <var class="term">needle</var> character string in the <var class="term">haystack</var> string, starting at the implicit or explicit starting position. If <var class="term">needle</var> is not found in <var class="term">haystack</var>, <var class="term">%position</var> is set to 0. </td></tr>
<tr><th>needle </th>
<tr><th>unicode</th>
<td>The character string to be located in the ''haystack'' string. </td></tr>
<td>The method object string.  Depending upon whether the <var>UnicodePositionIn</var> or <var>UnicodePositionOf</var> method is being used, <var class="term">unicode</var> may be either the:
<tr><th>haystack </th>
<ul><li>search target <var class="term">needle</var>, when called as <var>UnicodePositionIn</var>
<td>The character string in which ''needle'' is to be located. </td></tr>
<li>the search space <var class="term">haystack</var>, when called as <var>UnicodePositionOf</var></ul></td></tr>
<tr><th>Start=spos </th>
<tr><th>string</th>
<td>This optional, name-required argument is a number that specifies the position in the ''haystack'' string at which to start searching for the ''needle'' string. ''spos'' defaults to 1, meaning that the search begins at the first character in ''haystack''.</td></tr>
<td>The argument string.  Depending upon whether the <var>UnicodePositionIn</var> or <var>UnicodePositionOf</var> method is being used, <var class="term">string</var> may be either the:
<ul><li>search target <var class="term">needle</var>, when called as <var>UnicodePositionOf</var>
<li>the search space <var class="term">haystack</var>, when called as <var>UnicodePositionIn</var></ul></td></tr>
<tr><th>start</th>
<td>This optional, but <var>name-required</var> argument is a number specifying the position in the <var class="term">haystack</var> string at which to start searching for the <var class="term">needle</var> string. <var class="term">start</var> defaults to 1, meaning that the search begins at the first character in <var class="term">haystack</var>.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>The starting position must be a positive number.
<li>The <var class="term">start</var> position must be a positive number. A zero or negative number results in request cancellation. Specifying a <var class="term">start</var> position greater than the length of (<var class="term">haystack</var> plus one, minus the length of <var class="term">needle</var>) returns a zero, because there are not enough characters in <var class="term">haystack</var> to satisfy the search criteria.
A zero or negative number results in request cancellation.
<li><var>UnicodePositionIn</var> / <var>UnicodePositionOf</var> do exactly the same thing. The only difference between them is the meaning assigned to the method object <var class="term">unicode</var> and <var class="term">string</var> arguments.
Specifying a starting position greater than the length of ''haystack''
<ul><li>In <var>UnicodePositionOf</var>, the <var class="term">haystack</var> is the method object and the <var class="term">needle</var> is the argument <var class="term">string</var>.
plus one, minus the length of ''needle'', returns a zero,
<li>In <var>UnicodePositionIn</var> the method object <var class="term">unicode</var> and first argument <var class="term">string</var> meansings are reversed.</ul>
because there are not enough characters in ''haystack''
Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
to satisfy the search.
<li><var>UnicodePositionIn</var> / <var>UnicodePositionOf</var> are analogous to the String <var>intrinsics</var> <var>[[PositionIn and PositionOf (String functions)|PositionIn]]</var> and <var>[[PositionIn and PositionOf (String functions)|PositionOf]]</var>.
<li>The UnicodePositionOf and UnicodePositionIn methods do exactly the same thing.
<li><var>UnicodePositionIn</var> / <var>UnicodePositionOf</var> are available as of <var class="product">[[Sirius Mods|"Sirius Mods"]]</var> Version 7.5.
The only difference between them is that in UnicodePositionOf, the ''haystack''
is the method object and the ''needle'' is the first argument.
In UnicodePositionIn, the method object 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.
<li>The UnicodePositionOf and UnicodePositionIn methods are analogous to
the String intrinsic PositionIn and PositionOf methods (??[[PositionIn and PositionOf (String functions)|PositionIn and PositionOf]]).
</ul>
</ul>
==Examples==
==Examples==
 
<ol><li>
Some examples follow.
The following fragment contains three calls to UnicodePositionOf:
For information about the PrintText statement, see [[Intrinsic classes#printtext|this item]].
<ul>
<li>The following fragment contains three calls to UnicodePositionOf:
<p class="code">%s is unicode initial('This is a test')
<p class="code">%s is unicode initial('This is a test')
printText {%s:unicodePositionOf('is')}
[[PrintText statement|printText]] {%s:unicodePositionOf('is')}
printText {%s:unicodePositionOf('is', start=4)}
printText {%s:unicodePositionOf('is', start=4)}
printText {%s:unicodePositionOf('is', start=7)}
printText {%s:unicodePositionOf('is', start=7)}
</p>
</p>
The result is:
The result is:  
<p class="output">3
<p class="output">3
6
6
Line 70: Line 58:
0
0
</p>
</p>
<li>The following sequence, which uses the [[U (String function)|U function]] to
<li>The following sequence, which uses the [[U (String function)|U function]] to specify the Unicode trademark character (U+2122), produces a conversion error. The second statement fails when the method attempts to implicitly convert the Unicode <var class="term">needle</var> character to an EBCDIC String. The Unicode trademark character has no valid translation to EBCDIC.
specify the Unicode trademark character (U+2122), produces a conversion error.
The second statement fails when the method attempts to implicitly convert the
Unicode "needle" character to an EBCDIC String.
The Unicode trademark character has no valid translation to EBCDIC.
<p class="code">%u is unicode initial('Model 204&amp;#x2122;':U)
<p class="code">%u is unicode initial('Model 204&amp;#x2122;':U)
%posTM = %u:PositionOf('&amp;#x2122;':U)
%posTM = %u:PositionOf('&amp;#x2122;':U)
print %posTM
print %posTM
</p>
</p>
 
However, given %u as defined above, here is an alternative solution that uses other intrinsic methods to return the hex value of the trademark character:
However, given %u as defined above,
here is an alternative solution that uses other intrinsic methods
to return the hex value of the trademark character:
<p class="code">%pos is float
<p class="code">%pos is float
%pos = %u:UnicodeUntranslatablePosition
%pos = %u:[[UnicodeUntranslatablePosition (Unicode function)|UnicodeUntranslatablePosition]]
Printtext {~} is: -
printtext {~} is: -
   {%u:unicodeChar(%pos):unicodeToUtf16:stringToHex}
   {%u:[[UnicodeChar (Unicode function)|unicodeChar]](%pos):[[UnicodeToUtf16 (Unicode function)|unicodeToUtf16]]:[[StringToHex (String function)|stringToHex]]}
</p>
</p>


Line 92: Line 73:
<p class="output">%u:unicodeChar(%pos):unicodeToUtf16:stringToHex is: 2122
<p class="output">%u:unicodeChar(%pos):unicodeToUtf16:stringToHex is: 2122
</p>
</p>
</ul>
</ol>
 
==See also==
==See also==
{{Template:Unicode:UnicodePositionIn and UnicodePositionOf footer}}
{{Template:Unicode:UnicodePositionIn and UnicodePositionOf footer}}

Revision as of 08:33, 24 February 2011

The position of one string inside another (Unicode class)


These UnicodePositionIn and UnicodePositionOf functions return the numeric position of the first occurrence of one character string inside another (character case respected). The difference between the two methods is that for UnicodePositionIn, the method object string is located in the first argument string, whereas for UnicodePositionOf, the first argument string is located in the method object string. Which method is more convenient to use will be application dependent.

Syntax

%position = unicode:UnicodePositionIn( haystack, [Start= number])

%position = unicode:UnicodePositionOf( needle, [Start= number])

Syntax terms

%number A numeric variable to receive the position of the first occurrence of the needle character 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.
unicode The method object string. Depending upon whether the UnicodePositionIn or UnicodePositionOf method is being used, unicode may be either the:
  • search target needle, when called as UnicodePositionIn
  • the search space haystack, when called as UnicodePositionOf
string The argument string. Depending upon whether the UnicodePositionIn or UnicodePositionOf method is being used, string may be either the:
  • search target needle, when called as UnicodePositionOf
  • the search space haystack, when called as UnicodePositionIn
start This optional, but name-required argument is a number specifying the position in the haystack string at which to start searching for the 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 criteria.
  • UnicodePositionIn / UnicodePositionOf do exactly the same thing. The only difference between them is the meaning assigned to the method object unicode and string arguments.
    • In UnicodePositionOf, the haystack is the method object and the needle is the argument string.
    • In UnicodePositionIn the method object unicode and first argument string meansings are reversed.

    Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.

  • UnicodePositionIn / UnicodePositionOf are analogous to the String intrinsics PositionIn and PositionOf.
  • UnicodePositionIn / UnicodePositionOf are available as of "Sirius Mods" Version 7.5.

Examples

  1. The following fragment contains three calls to UnicodePositionOf:

    %s is unicode initial('This is a test') printText {%s:unicodePositionOf('is')} printText {%s:unicodePositionOf('is', start=4)} printText {%s:unicodePositionOf('is', start=7)}

    The result is:

    3 6 0

  2. The following fragment contains three calls to UnicodePositionIn:

    %s is unicode initial('spl') printText {%s:unicodePositionIn('splish splash')} printText {%s:unicodePositionIn('splish splash', start=4)} printText {%s:unicodePositionIn('splish splash', start=9)}

    The result is:

    1 8 0

  3. The following sequence, which uses the U function to specify the Unicode trademark character (U+2122), produces a conversion error. The second statement fails when the method attempts to implicitly convert the Unicode needle character to an EBCDIC String. The Unicode trademark character has no valid translation to EBCDIC.

    %u is unicode initial('Model 204&#x2122;':U) %posTM = %u:PositionOf('&#x2122;':U) print %posTM

    However, given %u as defined above, here is an alternative solution that uses other intrinsic methods to return the hex value of the trademark character:

    %pos is float %pos = %u:UnicodeUntranslatablePosition printtext {~} is: - {%u:unicodeChar(%pos):unicodeToUtf16:stringToHex}

    The result is:

    %u:unicodeChar(%pos):unicodeToUtf16:stringToHex is: 2122

See also