UnicodePositionIn and UnicodePositionOf (Unicode functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
(No difference)

Revision as of 18:24, 27 April 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 (the "needle") inside another ("the haystack"). The search respects character case. The difference between the two methods is that for UnicodePositionIn, the method object string is the "needle" and the first argument is the "haystack," whereas for UnicodePositionOf, the first argument string is the "needle" and the method object string is the "haystack." 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

%position A variable to receive the character position of the first occurrence of the "needle" Unicode string in the "haystack" Unicode string, starting at the implicit or explicit starting character position. If the "needle" is not found in the "haystack," %position is set to 0. Whether the method object or the first argument is the "needle" or "haystack" depends on the method that is called.
unicode The method object Unicode string; either:
  • the search target "needle," when called as UnicodePositionIn
  • the search space "haystack," when called as UnicodePositionOf
haystack In UnicodePositionIn, the search space Unicode string within which to look for the unicode method object (the "needle").
needle In UnicodePositionOf, the search target Unicode string to be searched for within the unicode method object (the "haystack").
Start An optional, but NameRequired, number specifying the character position in the "haystack" Unicode string at which to start searching for the "needle" Unicode 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 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™':U) %posTM = %u:PositionOf('™':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

    UnicodePositionIn and UnicodePositionOf are analogous to the PositionIn and PositionOf String methods.