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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I SPLIT a string into an array?

Status
Not open for further replies.

marcela24

Programmer
Feb 17, 2003
72
0
0
NL
Hi. I want to split a string like this: '999 #998 #994 #', by the '#' character into an array, that in this case will have 3 positios. How can I do this? (Please describe all the comands, including the array declaration. I´m totally newbie in Fox Pro). thanks for your help.
 
If you know that there will always be 3 elements you can use the DIMENSION statement to create an array with 3 elements. If not you'll have to start with DIMENSION[1] and redimension the array each time that you want to add a new member.

Loop through the string using the AT() function to find the position of the first # and then the SUBSTR() function to extract the characters up to that position.



Geoff Franklin
 
Ooops! Thanks for your help. I´ve already did it!
 
Sometimes I use fn. MEMLINES() and MLINE().

*-- convert # to Linefeed for proper function MLINE()
lcString = strtran(lcString, '#', chr(13))
for x=1 to memlines(lcString)
? mline(lcString)
endfor
 
Sorry:
line "? mline(lcString)" have to be "? mline(lcString,x)
 
I would do it this way, assuming that you always have a space before the #:

cString = "999 #998 #994 #"
cSeparator = " #" && Space + '#'
nCount = 0
DO WHILE NOT EMPTY(cString)
nCount = nCount + 1
DIMENSION aWord(nCount)
    aWord(nCount) = ATXLEFT(cString, cSeparator)
    cString = ATXRIGHT(cString, cSeparator)
ENDDO

This will work regardless whether the number of array elements is known or not.

See FAQ182-5975 for the code for these UDFs.




mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
For anyone who wants a packaged solution, I've posted my old SPLIT function: faq182-6039

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top