Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse A String With Any Delimiter

Usefull Functions & Procedures

Parse A String With Any Delimiter

by  Walid  Posted    (Edited  )
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]
Do CASE
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!"
Endcase

If !Empty(lcMessageText)
MessageBox(lcMessageText,lnMessageIcon,lcMessageTitle)
Return .F.
Endif
*// 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!"
MessageBox(lcMessageText,lnMessageIcon,lcMessageTitle)
ON ERROR &lcOldError
Return .F.
Endif
*// 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
If !("FOXTOOLS" $ UPPER(SET('LIBRARY')))
lcOldLib = SET("LIBRARY")
*//Make sure FoxTools.fll in the root directory of your application b4 issuing this command.
Set LIBRARY TO FOXTOOLS.FLL ADDI
llSetLibrary = .T.
Endif
Endif
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)
Endfor
Endif


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

[color blue]Use Example...[/color]

DIMENSION la[1]

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

la[1]="Walid"
la[2]="Magd"
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top