Miscellaneus String Functions


Here you go. A handful of nice little string functions. Please notice that some of the functions use each other. These are the functions:
  • StripC strips preceeding characters (e.g. blanks).
  • Rjust right justifys.
  • StrCtr centers a string into another.
  • StrIns inserts a string into another.
  • StrDel deletes part of a string.
  • StrChg changes a substring to another.

StripC: proc( inStr, inChar ) returns( var char(255) ) recursive;

dcl inStr      char(*),
    inChar     char;
		
dcl substr     builtin;
  
  if substr( inStr,1,1 ) = inChar
    then return( StripC( substr( inStr,2 ), inChar );
    else return( inStr );

end StripC;

WT Bailey from the UK has sent this alternative version of the StripC function. It doesn't use recursion, so I guess it is faster than the example above.

StripC: proc( inStr, inChar ) returns( var char(255) );

dcl inStr      char(*),    
    inChar     char;  
dcl i          fixed bin(15);
dcl substr     builtin;

i = verify(inStr, inChar);

if i > 0 then
  return( substr( inStr,i) );
else
  return('');

end StripC;
Top

Rjust: proc( inStr, inChar ) returns( var char(255) ) recursive;

dcl inStr      char(*),
    inChar     char;
		
dcl substr     builtin,
    length     builtin;

  if substr(inStr,length(inStr),1) = inChar
    then return( Rjust( substr(inStr,length(inStr),1 ) !!
                        substr(inStr,1,length(inStr) - 1),
                        inChar )
                );
    else return( inStr );
								
end Rjust;
Top

StrCtr: proc( inStr, inMaster ) returns( var char(255) );

dcl inStr      char(*),
    inMaster   char(*);
		
dcl outStr     var char(length(inMaster)),
    startPos   bin(15,0);
		
dcl substr     builtin,
    length     builtin;

  if length(inMaster) > length(inStr)
    then do;
		  outStr = inMaster;
      startPos = (length(outStr) / 2) - (length(inStr) / 2) + 2;
			substr(outStr,startPos,length(inStr)) = inStr;
			return( outStr );
    end;
    else
      return( inStr );
								
end StrCtr;
Top

StrIns: proc( inStr, inMaster, pos, mark ) returns( var char(255) );

/* The parameter 'mark' is used to determine wether we are going */
/* to insert a word or a substring                               */

dcl inStr      char(*),
    inMaster   char(*),
    pos        bin(15,0),
    mark       char;

dcl wrd        char static init('W'),
    str        char static init('S');
		
dcl substr     builtin,
    length     builtin;

  if pos > length(inMaster)
    then pos = length(inMaster);
  
  select;
    when( (pos > 1) & (pos < length(inMaster)) )
      if mark = wrd
        then return( substr(inMaster,1,pos-1) !!
                     inStr !!' '!!
                     substr(inMaster,pos )
                   );
        then return( substr(inMaster,1,pos-1) !!
                     inStr !!
                     substr(inMaster,pos )
                   );
    when( pos = 1 )
      if (substr(inMaster,1,1) = '') ! (mark = str)
        then return( inStr !! inMaster );
        else return( inStr !!' '!! inMaster );
    when( pos = length(inMaster) )
      if (substr(inMaster,pos,1) = '') ! (mark = str)
        then return( inStr !! inMaster );
        else return( inStr !!' '!! inMaster );
    other
      return('Error in arg: POS ='!! pos );
  end; /*select*/
									
end StrIns;
Top

StrDel: proc( inStr, inMaster, mark ) returns( var char(255) );

dcl inStr      char(*),
    inMaster   char(*),
    mark       char;
		
dcl pos        bin(15,0),
    xChar      bin(15,0),
    outStr     var char(255);

/* The parameter 'mark' is used to determine wether we are going */
/* to delete a word or a substring                               */

dcl wrd        char static init('W'),
    str        char static init('S');

dcl substr     builtin,
    length     builtin,
    index      builtin;

  if mark = wrd  
    then do;
      pos = index(' '!! inMaster !!' ',inStr);
      xChar = 1;
    end;
    else do;
      pos = index(inMaster,inStr);
      xChar = 0;
    end;

  select;
    when( pos > 1 )
      if ((pos + xChar) + length(inStr)) < length(inMaster)
        then return( substr(inMaster,1,(pos - 1)) !!
                     substr(inMaster,(pos + xChar + length(inStr)))
                   );
        else return( substr(inMaster,1,(pos - 1)) );
    when( pos = 1 )
        if inStr = inMaster
          then return('');
          else return( substr(inMaster,(length(instr) + 1 + xChar)) );
    other
      return( inMaster );
  end; /*select*/

end StrDel;
Top

StrChg: proc( inoldStr, inMaster, inNewStr, mark )
        returns( var char(255) );

/* Remove inOldStr in inMaster and replace it with inNewStr      */

dcl inOldStr   char(*),
    inMaster   char(*),
    inNewStr   char(*),
    mark       char;

dcl pos        bin(15,0),
    outMaster  char(length(inMaster));

/* The parameter 'mark' is used to determine wether we are going */
/* to change a word or a substring                               */

dcl wrd        char static init('W'),
    str        char static init('S');

dcl substr     builtin,
    length     builtin,
    index      builtin;

  if inOldStr = inNewStr
    then return( inMaster );
    else do;
      pos = index(inMaster,inOldStr);
      if pos > 1
        then do;
          outMaster = StrDel(inOldStr,inMaster,mark);
          return( StrIns(inNewStr,outMaster,pos,mark) );
        end;
        else
          return( inMaster );
    end;

end StrChg;
Top


Back