Extracting Values From String Data

When you are working with data stored as a string, you can extract substrings from the total string. This extraction is done by specifying the offset within the string, indicating from which position you want to extract the substring.

The format for this is:

Source[Start {:Num|,End}]

String Extraction Parameters
Parameter Meaning
Start Position number from which to start extracting.
, End Position number at which to stop extracting.
: Num The number of positions to extract from start .

Remember to take great care in the naming of local variable used as the string extraction parameter. A single lettered variable name of one of the following letters, IFRYMDHNSTAWX, will generate a warning. They will be ignored as they represent a mathematical or date function (see previous sections). These mathematical and date functions have precedence before any other values.

Note:  Using string extraction on a string containing Uniface frame markers will not copy the frame markers. You must copy the whole string with an assignment statement.

Rules for String Extraction

The following rules apply when string extraction is performed:

  • The first position in the string is always numbered 1.
  • The resulting string is always empty (that is, an empty string is returned) if any of the following are true:
    • Start,End, or Num is less than 1
    • Start is greater than End
    • Start is greater than the available number of characters
  • Start, End or Num can be a constant, a variable, $status, $result, or an expression. For example, the following is a legal statement:
    $1 = NAME[$result+1]
    
  • If End or Num has a value greater than the available character positions, the characters are extracted to the end of the string.
  • If string extraction is used on a nonstring source (for example, on a Date or Numeric field), the value is first converted to string format according to the default display format.
  • If string extraction is applied to a field containing subfields, for the purposes of the extraction, Uniface treats the contents of the complete field as a single string (including the subfield separators as part of the string).
  • If string extraction is applied to a string that contains operators and opening braces (such as +, -> and {), these are treated as strings unless the expression is enclosed in parentheses.
  • String extraction always has a lower priority than indirection. For example, the following statement combines indirection and string extraction:
    $2 = @$1[2:5]

    is equivalent to the following statements:

    $TEMP$ = @$1
    $2 = $TEMP$[2:5]

Other ProcScript Instructions

In addition to string extraction, Uniface provides the $length and $scan instructions. The $length instruction determines the length of a string and the $scan instruction returns the starting position of a string or pattern in the source string.

Examples using string extraction
Statement Meaning Result

NAME = "HOLLERITH"

   

$1 = NAME[4,8]

Extract positions 4 through 8 LERIT

$1 = NAME[3:3]

Extract positions 3 through 5 LLE

$10 = 2

   

$1 = NAME[$10:4]

Extract positions 2 through 5 OLLE

$1 = NAME[3]

Extract positions 3 though 9 LLERITH

length NAME

Get length of NAME 9

$1 = NAME[$result]

Extract last character of NAME H

scan NAME, "LL?"

Get position of string matching ‘LL?’ 3

$10 = $result+2

Get position of character following ‘LL’ 5

$1 = NAME[$10:1]

Extract character following ‘LL’ E

$1 = "AMSTER123DAM"

$1 contains letters and numbers  

scan $1,"#"

Get position of first digit in $1 7

if ($result > 0)

$1 contains numeric data  

     $3 = $1[$result]

Remove leading nondigits in $1 123DAM

     $2 = $number($3)

Convert to number 123

else

   

     $2 = ""

No numeric data in $1  

endif

   

Numeric t_start

   

t_start = 3

   

$1 = NAME[t_start]

Extracts position 3 through 9 LLERITH