Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Usefull Functions & Procedures

Parse A String With Any Delimiter by Walid
Posted: 19 Jun 02

Here is a generic function I use for this purpose, I realized that the Zip code id not separated by comma from the state, but it will be easy to extract it from the last element of the array this function returns.

*// Author        : IIF(This Code Is Working Ok, Walid Magd, I Have No Idea) :)
*// Parameters    : 1) A string consists of some words separated by delimiter in
*//                    the first parameter lcString.
*//                 2) Array by reference in the second parameter
*//                    laArray.
*//                 3) Optional. Specifies the character used to delimit character groups in lcString.
*// Functionality : 1) Detect if any specific delimiting character was passed in the 3rd parameter
*//                    If not, this function by default assumes that words are delimited by spaces or tabs
*//                 2) Parsing lcString to detect How many words in this string. This number will be used
*//                    to assign the right dimension for the array.
*//                 3) Extract every word and assign it to new array element.
*// Returns       : True  if the array successfully Re dimensioned and populated with the words in the string.
*//               : False if any error encountered
FUNCTION String2Array
Parameters lcString, laArray, lcDelimiter
Local lcMessageText,lnMessageIcon,lcMessageTitle,lcOldError,lnAlen,llError
lcMessageTitle = "Error.."
lnMessageIcon  = 0+16+0
*// Parameter checking procedure [Start]
  lcMessageText  = Space(0)
  Case PCOUNT() < 2
    lcMessageText = "Too few parameters"
  Case PCOUNT() > 3
    lcMessageText = "Too many parameters"
  Case EMPTY(lcString)
    lcMessageText = "Can't parse an empty string!"
  Case PCOUNT() = 3 AND VARTYPE(lcDelimiter) != 'C'
    lcMessageText = "The delimiter must be of type character!"  
  CASE PCOUNT() = 3 AND LEN(ALLTRIM(lcDelimiter)) > 1
    lcMessageText = "The delimiter must be only one character!"    

If !Empty(lcMessageText)
  Return .F.
*// Make sure that laArray is variable of type Array [Start]
lcOldError = ON('ERROR')
llError = .F.
ON ERROR llError = .T.
lnAlen = ALEN(laArray)
IF llError  
  lcMessageText = "Can't manipulate the array, make sure that it defined properly and passed by reference!"
  ON ERROR &lcOldError
  Return .F.
*// Make sure that laArray is variable of type Array [End  ]

 *// Free memory
RELEASE lcMessageText,lnMessageIcon,lcMessageTitle,lcOldError,lnAlen,llError
ON ERROR &lcOldError
*// Parameter checking procedure [End  ]

Local llBuiltInFunction, llSetLibrary, lnWordCount, lcExpWordCount, lcExpWordNum, lcOldLib
Store SPACE(0) TO lcExpWordCount, lcExpWordNum, lcOldLib
llSetLibrary      = .F.
lnWordCount       = 0
lnIndex           = 0
llBuiltInFunction = IIF(Left(Version(4),2) == "07",.T.,.F. )
lcDelimiter       = IIF(PCOUNT()=2 OR EMPTY(lcDelimiter),SPACE(0),ALLTRIM(lcDelimiter))
If !llBuiltInFunction
    lcOldLib = SET("LIBRARY")
    *//Make sure FoxTools.fll in the root directory of your application b4 issuing this command.
    llSetLibrary = .T.
lcExpWordCount   = IIF(llBuiltInFunction,[GETWORDCOUNT(],[Words(])

lcExpWordCount   = lcExpWordCount + "lcString"+IIF(EMPTY(lcDelimiter),[)],[,+lcDelimiter)] )

lcExpWordNum     = IIF(llBuiltInFunction,[GETWORDNUM(],[WordNum(])
lcExpWordNum     = lcExpWordNum + "lcString, lnIndex"+IIF(EMPTY(lcDelimiter),[)],[,+lcDelimiter)] )

lnWordCount = EVAL(lcExpWordCount)
If lnWordCount > 0
  Dimension laArray[lnWordCount]  && Redimensioning the array to the right number
  For lnIndex = 1 TO lnWordCount

    laArray[lnIndex] = EVAL(lcExpWordNum)

*// Restore the old sitting b4 you go
If llSetLibrary
  Set LIBRARY TO &lcOldLib

Use Example...


lcString = "Walid Magd"

llSuccessfulCall = string2array(lcString,@la,",")

This will return only one element la[1]="Walid Magd"

llSuccessfulCall = string2array(lcString,@la)

This will return array has two elements


Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close