home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a009 / 6.ddi / SAMPLE.LIF / STRING.PRG < prev    next >
Encoding:
Text File  |  1991-04-14  |  2.7 KB  |  117 lines

  1. /***
  2. *
  3. *  String.prg
  4. *  Sample user-defined functions for processing character strings
  5. *
  6. *  Copyright, Nantucket Corporation, 1990
  7. *
  8. *  NOTE: compile with /n/w/a/m
  9. */
  10.  
  11.  
  12.  
  13. /***
  14. *  CityState( <cCity>, <cState>, <cZip> ) --> cString
  15. *  Format city, state and zip variables into a single string
  16. *
  17. */
  18. FUNCTION CityState( cCity, cState, cZip )
  19.    RETURN RTRIM(cCity) + ", " + RTRIM(cState) + "  " + cZip
  20.  
  21.  
  22.  
  23. /***
  24. *  ListAsArray( <cList>, <cDelimiter> ) --> aList
  25. *  Convert a delimited string to an array
  26. *
  27. */
  28. FUNCTION ListAsArray( cList, cDelimiter )
  29.    LOCAL nPos
  30.    LOCAL aList := {}                            // Define an empty array
  31.  
  32.    IF cDelimiter = NIL
  33.       cDelimiter := ","
  34.    ENDIF
  35.    //
  36.    DO WHILE (nPos := AT(cDelimiter, cList)) != 0
  37.       AADD(aList, SUBSTR(cList, 1, nPos - 1))   // Add a new element
  38.       cList := SUBSTR(cList, nPos + 1)
  39.    ENDDO
  40.    AADD(aList, cList)                           // Add final element
  41.    //
  42.    RETURN aList                                 // Return the array
  43.  
  44.  
  45.  
  46. /***
  47. *  Occurs( <cSearch>, <cTarget> ) --> nCount
  48. *  Determine the number of times <cSearch> is found in <cTarget>
  49. *
  50. */
  51. FUNCTION Occurs( cSearch, cTarget )
  52.    LOCAL nPos, nCount := 0
  53.    DO WHILE !EMPTY( cTarget )
  54.       IF (nPos := AT( cSearch, cTarget )) != 0
  55.          nCount++
  56.          cTarget := SUBSTR( cTarget, nPos + 1 )
  57.       ELSE     
  58.          // End of string
  59.          cTarget := ""
  60.       ENDIF
  61.    ENDDO
  62.    RETURN nCount
  63.  
  64.  
  65.  
  66. /***
  67. *  Proper( <cString> ) --> cProper
  68. *  Capitalize each word in a string
  69. *
  70. */
  71. FUNCTION Proper( cString )
  72.    LOCAL nPos, cWord, cProper := ""
  73.    DO WHILE !EMPTY( cString )
  74.       IF (nPos := AT(" ", cString)) != 0
  75.          cWord := SUBSTR( cString, 1, nPos )
  76.          cString := SUBSTR( cString, nPos + 1 )
  77.       ELSE     
  78.          // End of string
  79.          cWord := cString
  80.          cString := ""
  81.       ENDIF
  82.       cProper := cProper + UPPER(SUBSTR(cWord, 1, 1)) + SUBSTR(cWord, 2)
  83.    ENDDO
  84.    RETURN cProper
  85.  
  86.  
  87. #ifdef NOTDEF
  88.  
  89. // duplicates StrZero() defined in EXAMPLEP.PRG
  90.  
  91. /***
  92. *   StrZero( <nNumber>, <nLength>, <nDecimals> ) --> cNumber
  93. *   Convert a numeric to a string padded with leading zeros
  94. *
  95. */
  96. FUNCTION StrZero( n, nLength, nDecimals )
  97.    LOCAL cNumber
  98.    IF PCOUNT() = 3
  99.       cNumber = STR(n, nLength, nDecimals)
  100.    ELSEIF PCOUNT() = 2
  101.       cNumber = STR(n, nLength)
  102.    ELSE
  103.       cNumber = STR(n)
  104.    ENDIF
  105.  
  106.    IF "-" $ cNumber
  107.       // Negative number, move the minus sign in front of zeros
  108.       RETURN "-" + REPLICATE("0", LEN(cNumber) - LEN(LTRIM(cNumber))) +;
  109.             SUBSTR(cNumber, AT("-", cNumber) + 1)
  110.    ELSE
  111.       // Positive number
  112.       RETURN REPLICATE("0", LEN(cNumber) - LEN(LTRIM(cNumber))) +;
  113.             LTRIM(cNumber)
  114.    ENDIF
  115.  
  116. #endif
  117.