HP 3000 Manuals

String Functions [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation


HP Pascal/iX Reference Manual

String Functions 

String functions may be used to manipulate string expressions, variables,
or literals.  A string expression may consist of a string literal, a
string variable, a string constant, a function result that is a string,
or an expression formed with the concatenation operator.

Note that strings must be initialized just like any other variable.  The
string functions and procedures assume that the string parameters contain
valid information.  The string functions str, strlen, strltrim, strmax,
strpos, strrpt, or strrtrim, are defined by HP Pascal and are described
on subsequent pages.

str 

Usage 

     str(s, p, e) 

Arguments 

s          A string expression.

p          An integer expression indicating the index of the starting
           character.

e          An integer expression indicating the length of the substring.

Description 

The function str(s, p, e) returns the portion of s which starts at s [ p 
] and is of length e.  The result is type string, and may be used as a
string expression.  It is an error if strlen(s) is less than p+ (e-1).

Example 

     VAR
        i: integer;
        wish_list: string[132];
        granted: string[5];

     BEGIN
        .
        i:= 13;
        wish_list:= 'wish1 wish2 wish3 wish4 wish5';
        granted:= str(wish_list,i,5);        { Selects the 3rd wish. }
                                             { Granted is 'wish3'.   }
     END.

strlen 

Usage 

     strlen(s) 

Argument 

s          A string expression.

Description 

The function strlen(s) returns the current length of the string or PAC
expression s.  If s is not initialized, strlen(s) is undefined.

Example 

     VAR
        ars, vita: string[132];
        b: boolean;

     BEGIN
        .
        ars:= 'HELLO';
        vita:= 'TO YOU';
        IF strlen(ars) > strlen(vita) THEN
           b:= true
        ELSE
           b:=false;
            .
        writeln (strlen(ars):2,strlen(vita):2);
     END.

Output:

     5 6

strltrim 

Usage 

     strltrim(s) 

Argument 

s          A string expression.

Description 

The function strltrim(s) returns a string consisting of s trimmed of all
leading blanks.  The function strrtrim trims trailing blanks.

Example 

     VAR
       s: string[80];
     BEGIN
        .
       s:= '          abc';
       s:=strltrim(s);        {s is now 'abc'}
        .                     {strlen(s) = 3 }
     END.

strmax 

Usage 

     strmax(s) 

Argument 

s          A string variable.

Description 

The function strmax(s) returns the maximum length of s.  Strmax is useful
for finding the maximum length of VAR string parameters whose maximum is
not determined until run time. 

Example 

     VAR
        s: string[15];

     BEGIN
        s:= '     ABCDE     ';
        IF strlen(s) = strmax(s) THEN
           BEGIN
              s:= strltrim(s);
              s:= strrtrim(s);
           END;

        writeln (s,strmax(s):3);
        .
     END.

Output:

     ABCDE 15

strpos 

Usage 

     strpos(s1, s2) 

Arguments 

s1         A string expression.

s2         A string expression.

Description 

The function strpos(s1, s2) returns the integer index of the position of
the first occurrence of s2 in s1.  If s2 is not found, zero is returned.
If the length of s2 is zero, the result is 1.


NOTE Some HP Pascal implementations have the order of the two parameters reversed. Also, your implementation may have a compiler option that reverses the order of parameters.
Example CONST separator = ' '; VAR i: integer; names: string[80]; BEGIN . names:= 'Jon Jill Ruth Marnie Bob Joan Wendy'; i:= strpos (names,separator); { i = 4 } IF i <> 0 THEN strdelete(names,1,i); { deletes first name } . i:= (strpos(names,'Ron')); { i = 0 } END strrpt Usage strrpt(s, n) Arguments s A string expression. n An integer expression indicating the number of repetitions where n must be greater than or equal to zero. Description The function strrpt(s, n) returns a string composed of s repeated n times. If n is 0, a zero-length string is returned. Example CONST one = '1'; VAR b_num: string[12]; BEGIN . b_num:= strrpt(one,strmax(b_num)); { b_num is '111111111111' } b_num:= strrpt ('a',10); { b_num is 'aaaaaaaaaa' } . END. strrtrim Usage strrtrim(s) Argument s A string expression. Description The function strrtrim(s) returns a string consisting of s trimmed of trailing blanks. Leading blanks are stripped by the function strltrim. Example VAR s: string[80]; BEGIN . s:= 'abc '; . s:= strrtrim(s); { s is now 'abc' } . { strlen(s) = 3 } END.


MPE/iX 5.0 Documentation