home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / STRING.PR_ / STRING.PR
Text File  |  1995-06-20  |  2KB  |  115 lines

  1. /***
  2. *
  3. *  String.prg
  4. *
  5. *  Sample user-defined functions for processing character strings
  6. *
  7. *  Copyright (c) 1995, Computer Associate International Inc.
  8. *  All rights reserved.
  9. *
  10. *  NOTE: Compile with /a /m /n /w
  11. *
  12. */
  13.  
  14. #include "Common.ch"
  15.  
  16.  
  17. /***
  18. *
  19. *  CityState( <cCity>, <cState>, <cZip> ) --> cString
  20. *
  21. *  Format city, state and zip variables into a single string
  22. *
  23. */
  24. FUNCTION CityState( cCity, cState, cZip )
  25.    RETURN ( RTRIM( cCity ) + ", " + RTRIM( cState ) + "  " + cZip )
  26.  
  27.  
  28.  
  29. /***
  30. *
  31. *  ListAsArray( <cList>, <cDelimiter> ) --> aList
  32. *
  33. *  Convert a delimited string to an array
  34. *
  35. */
  36. FUNCTION ListAsArray( cList, cDelimiter )
  37.  
  38.    LOCAL nPos              // Position of cDelimiter in cList
  39.    LOCAL aList := {}       // Define an empty array
  40.  
  41.    DEFAULT cDelimiter TO ","
  42.  
  43.    // Loop while there are more items to extract
  44.    DO WHILE ( nPos := AT( cDelimiter, cList )) != 0
  45.  
  46.       // Add the item to aList and remove it from cList
  47.       AADD( aList, SUBSTR( cList, 1, nPos - 1 ))
  48.       cList := SUBSTR( cList, nPos + 1 )
  49.  
  50.    ENDDO
  51.    AADD( aList, cList )                         // Add final element
  52.  
  53.    RETURN ( aList )                             // Return the array
  54.  
  55.  
  56.  
  57. /***
  58. *
  59. *  Occurs( <cSearch>, <cTarget> ) --> nCount
  60. *
  61. *  Determine the number of times <cSearch> is found in <cTarget>
  62. *
  63. */
  64. FUNCTION Occurs( cSearch, cTarget )
  65.  
  66.    LOCAL nPos
  67.    LOCAL nCount := 0
  68.  
  69.    DO WHILE !EMPTY( cTarget )
  70.  
  71.       IF ( nPos := AT( cSearch, cTarget )) != 0
  72.          nCount++
  73.          cTarget := SUBSTR( cTarget, nPos + 1 )
  74.       ELSE
  75.          // End of string
  76.          cTarget := ""
  77.       ENDIF
  78.  
  79.    ENDDO
  80.  
  81.    RETURN ( nCount )
  82.  
  83.  
  84.  
  85. /***
  86. *
  87. *  Proper( <cString> ) --> cProper
  88. *
  89. *  Capitalize each word in a string
  90. *
  91. */
  92. FUNCTION Proper( cString )
  93.  
  94.    LOCAL nPos
  95.    LOCAL cWord
  96.    LOCAL cProper := ""
  97.  
  98.    DO WHILE !EMPTY( cString )
  99.  
  100.       IF ( nPos := AT( " ", cString )) != 0
  101.          cWord   := SUBSTR( cString, 1, nPos )
  102.          cString := SUBSTR( cString, nPos + 1 )
  103.       ELSE
  104.          // End of string
  105.          cWord   := cString
  106.          cString := ""
  107.       ENDIF
  108.  
  109.       cProper := cProper + UPPER( SUBSTR( cWord, 1, 1 )) + SUBSTR( cWord, 2 )
  110.  
  111.    ENDDO
  112.  
  113.    RETURN ( cProper )
  114.  
  115.