String Text

The following code is from Mike Haston




   Booth, 

   I like the site. First time I've seen it. If you don't mind I'm going to
   try out a few of your code snippets. Here are a couple string type things
   that I have that I see you've written as well. Maybe you can modify them
   and they can help sometime? One is procedures and the other just random
   tests. 

   Mike 


     h nomain
     h debug( *yes )
     h optimize( *full )
     h option( *nodebugio : *srcstmt )

     é*****************************************************************
     é*¹Procedure Prototypes                                         é*
     é*****************************************************************
     d #centerText     pr           256a
     d inString                     256a   varying
     d textLength                     3s 0

     d #cvtToLower     pr           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

     d #cvtToUpper     pr           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

     d #standardize    pr          1024a
     d inString                    1024a   const
     d changeFrom                  1024a   const
     d changeTo                    1024a   const

     é*****************************************************************
     é*¹Global Variables                                             é*
     é*****************************************************************
     d lo              c                   const('abcdefghijklmnopqrstuvwxyz')
     d up              c                   const('ABCDEFGHIJKLMNOPQRSTUVWXYZ')

     é**********************************************************************
     é*¹Procedure   - #centerText                                         é*
     é*¹Description - Center text in a string                             é*
     é*¹Input       - textIn 256a - text to be centered                   é*
     é*¹            - fieldLength 3,0 - length of field to center it in   é*
     é*¹Output      - string 256a - make sure to MOVEL or EVAL output     é*
     é**********************************************************************
     p #centerText     b                   export

     d #centerText     pi           256a
     d inString                     256a   varying
     d fldLen                         3s 0

     d i               s              2  0
     d textOut         s            256a

      /free

       i = (( fldLen - %len( inString )) / 2 ) + 1;
       %subst( textOut : i ) = inString;
       return  textOut;

      /end-free

     p #centerText     e

      /eject
     é**********************************************************************
     é*¹Procedure   - #cvtToLower                                         é*
     é*¹Description - Convert a string to lower case letters              é*
     é*¹Input       - string 256a                                         é*
     é*¹            - starting position within string *optional*          é*
     é*¹              the default on nopass will be the first position    é*
     é*¹Output      - string 256a - all lower case                        é*
     é**********************************************************************
     p #cvtToLower     b                   export

     d #cvtToLower     pi           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

      /free

       if %parms = 1;
         return   %xlate( up : lo : inString );
       else;
         return   %xlate( up : lo : inString : start@ );
       endif;

      /end-free

     p #cvtToLower     e

      /eject
     é**********************************************************************
     é*¹Procedure   - #cvtToUpper                                         é*
     é*¹Description - Convert a string to upper case letters              é*
     é*¹Input       - string 256a                                         é*
     é*¹            - starting position within string *optional*          é*
     é*¹              the default on nopass will be the first position    é*
     é*¹Output      - string 256a - all upper case                        é*
     é**********************************************************************
     p #cvtToUpper     b                   export

     d #cvtToUpper     pi           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

      /free

       if %parms = 1;
         return   %xlate( lo : up : inString );
       else;
         return   %xlate( lo : up : inString : start@ );
       endif;

      /end-free

     p #cvtToUpper     e

      /eject
     é**********************************************************************
     é*¹Procedure   - #standardize                                        é*
     é*¹Description - Receive a character string and convert any special  é*
     é*¹              characters to acceptable characters.                é*
     é*¹              Note: changeFrom / changeTo strings must be in the  é*
     é*¹                    matching order so that the correct characters é*
     é*¹                    are changed to their correct conterparts.     é*
     é*¹Input       - inString - string to be manipulated                 é*
     é*¹            - changeFrom - change from this character             é*
     é*¹            - changeTo - change to this character                 é*
     é*¹Output      - outString                                           é*
     é**********************************************************************
     p #standardize    b                   export

     d #standardize    pi          1024a
     d inString                    1024a   const
     d changeFrom                  1024a   const
     d changeTo                    1024a   const

      /free

       return   %xlate( changeFrom : changeTo : inString );

      /end-free

     p #standardize    e







Here's Mike's other code
     é*****************************************************************
     é*¹Procedure Prototypes                                         é*
     é*****************************************************************
     d #centerText     pr           256a
     d inString                     256a   varying
     d textLength                     3s 0

     d #cvtToLower     pr           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

     d #cvtToUpper     pr           256a
     d inString                     256a   varying
     d start@                         3s 0 options( *nopass )

     d #standardize    pr          1024a
     d inString                    1024a   const
     d changeFrom                  1024a   const
     d changeTo                    1024a   const

     é**********************************************************************
     d testString      s            256a   varying
     d len             s              3s 0 inz(80)
     d start@          s              3s 0 inz(5)
     d numOfTimes      s              3s 0                        
     d string          s            500a   varying                

      /free

       testString = 'Center me in the field please';
       testString = #centerText( testString : len );
       testString = #cvtToLower( testString );
       testString = #cvtToUpper( testString : start@ );
       testString = #standardize( '.#.xyz.~' : 'xyz#~' : 'aBc&*' );
       return;

      /end-free

       * scans for '#' and deletes it or replaces it with ''.

      c                   eval      numOfTimes = %len( string )                    
      c                   do        numOfTimes                                     
      c                   eval      pos = %scan( '#' : string )                    
      c                   if        pos = 0                                        
      c                   leave                                                    
      c                   endif                                                    
      c                   eval      string = %replace( '' : string : pos : 1)      
      c                   enddo