UnicodePositionIn and UnicodePositionOf (Unicode functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 9: Line 9:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%number</th>
<tr><th>%number</th>
<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>
<td>A numeric variable to receive one of the following:
<ul><li>The position of the first occurrence of the <var class="term">unicode</var> "needle" string target within the <var class="term">haystack</var> string (<var>UnicodePositionIn</var>).
<li>The position of the first occurrence of the <var class="term">needle</var> string target within the <var class="term">unicode</var> string "haystack" (<var>UnicodePositionOf</var>).
<li>0, if the "target" is not found.
</ul></td></tr>
<tr><th>unicode</th>
<tr><th>unicode</th>
<td>The method object Unicode 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:
<td>The method object <var>Unicode</var> string; either:
<ul><li>search target "needle," when called as <var>UnicodePositionIn</var>
<ul><li>the search target "needle," when called as <var>UnicodePositionIn</var>
<li>the search space "haystack," when called as <var>UnicodePositionOf</var></ul></td></tr>
<li>the search space "haystack," when called as <var>UnicodePositionOf</var></ul></td></tr>
<tr><th>haystack</th>
<tr><th>haystack</th>
<td>The search space string within which to look for the <var class="term">unicode</var> method object (the "needle").  
<td>The search space <var>Unicode</var> string within which to look for the <var class="term">unicode</var> method object (the "needle").  
<tr><th>needle</th>
<tr><th>needle</th>
<td>The search target string to be looked for within the <var class="term">unicode</var> method object (the "haystack").</td></tr>
<td>The search target <var>Unicode</var> string to be looked for within the <var class="term">unicode</var> method object (the "haystack").</td></tr>
<tr><th>Start</th>
<tr><th>Start</th>
<td>This optional, but <var>[[Methods#Named parameters|NameRequired]]</var> argument is a number specifying the position in the haystack string at which to start searching for the needle string. <var>Start</var> defaults to 1, meaning that the search begins at the first character in the haystack.</td></tr>
<td>This optional, but <var>[[Methods#Named parameters|NameRequired]]</var>, argument is a number specifying the position in the "haystack" string at which to start searching for the "needle" string. <var>Start</var> defaults to 1, meaning that the search begins at the first character in the "haystack."</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>The <var>Start</var> position must be a positive number.  A zero or negative number results in request cancellation.  Specifying a <var>Start</var> position greater than the length of the "haystack" 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.
<li>The <var>Start</var> position must be a positive number.  A zero or negative number results in request cancellation.  Specifying a <var>Start</var> position greater than the length of the "haystack" plus one, minus the length of <var class="term">needle</var> returns a zero, because there are not enough characters in the "haystack" to satisfy the search criteria.
<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.
<li><var>UnicodePositionIn</var> and <var>UnicodePositionOf</var> do exactly the same thing,
<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>.
but they swap the roles of the method object and the first argument.
<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>
Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
<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><var>UnicodePositionIn</var> and <var>UnicodePositionOf</var> are analogous to the <var>[[PositionIn and PositionOf (String functions)|PositionIn]]</var> and <var>[[PositionIn and PositionOf (String functions)|PositionOf]]</var> <var>String</var> methods.
<li><var>UnicodePositionIn</var> / <var>UnicodePositionOf</var> are available as of <var class="product">[[Sirius Mods]]</var> Version 7.5.
<li><var>UnicodePositionIn</var> and <var>UnicodePositionOf</var> are available as of <var class="product">[[Sirius Mods]]</var> Version 7.5.
</ul>
</ul>


==Examples==
==Examples==
<ol><li>
<ol><li>
The following fragment contains three calls to UnicodePositionOf:
The following fragment contains three calls to <var>UnicodePositionOf</var>:
<p class="code">%s is unicode initial('This is a test')
<p class="code">%s is unicode initial('This is a test')
[[PrintText statement|printText]] {%s:unicodePositionOf('is')}
[[PrintText statement|printText]] {%s:unicodePositionOf('is')}
Line 46: Line 49:
0
0
</p>
</p>
<li>The following fragment contains three calls to UnicodePositionIn:
<li>The following fragment contains three calls to <var>UnicodePositionIn</var>:
<p class="code">%s is unicode initial('spl')
<p class="code">%s is unicode initial('spl')
printText {%s:unicodePositionIn('splish splash')}
printText {%s:unicodePositionIn('splish splash')}
Line 58: Line 61:
0
0
</p>
</p>
<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.
<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 <var>Unicode</var> <var class="term">needle</var> character to EBCDIC. 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 <code>%u</code> as defined above, here is an alternative solution that uses other [[Intrinsic classes|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 (Unicode function)|UnicodeUntranslatablePosition]]
%pos = %u:[[UnicodeUntranslatablePosition (Unicode function)|UnicodeUntranslatablePosition]]

Revision as of 23:32, 1 March 2011

The position of one string inside another (Unicode class)


The 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 one of the following:
  • The position of the first occurrence of the unicode "needle" string target within the haystack string (UnicodePositionIn).
  • The position of the first occurrence of the needle string target within the unicode string "haystack" (UnicodePositionOf).
  • 0, if the "target" is not found.
unicode The method object Unicode string; either:
  • the search target "needle," when called as UnicodePositionIn
  • the search space "haystack," when called as UnicodePositionOf
haystack The search space Unicode string within which to look for the unicode method object (the "needle").
needle The search target Unicode string to be looked for within the unicode method object (the "haystack").
Start This optional, but NameRequired, 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 the "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 the "haystack" plus one, minus the length of needle returns a zero, because there are not enough characters in the "haystack" to satisfy the search criteria.
  • UnicodePositionIn and UnicodePositionOf do exactly the same thing, but they swap the roles of the method object and the first argument. Which method is preferable will depend on the application, and, in many cases, it will be quite arbitrary which one is used.
  • UnicodePositionIn and UnicodePositionOf are analogous to the PositionIn and PositionOf String methods.
  • UnicodePositionIn and 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 EBCDIC. 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