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 to split string?

Status
Not open for further replies.

marcela24

Programmer
Feb 17, 2003
72
0
0
NL
How can i split a string by using a single character? Is there a function for that?

For example:
HOW-ARE-YOU-?
I want to split this string in four words, using '-' as a separator.

Thanks.
 
I am affraid you have to code it yourself, there is no any similar function to VFPx ALINES() in FP DOS.
 
Hi,

This code uses AT() and SUBSTR() functions as suggested above, and stores the result in array aWords().
Code:
cString = "HOW-ARE-YOU-?"
cSeparator = "-"
nWords = occurs(cSeparator, cString) + 1
dimension aWords(nWords)
= split()
display memo like aWords

procedure split
nWordNo = 1
do while nWordNo < nWords
   nPos = at(cSeparator, cString)
   aWords(nWordNo) = left(cString, nPos - 1)
   cString = substr(cString, nPos + 1)
   nWordNo = nWordNo + 1
enddo
aWords(nWordNo) = cString
Just ask if you need more help.
 
I usually write my word separators like this:

cString = "HOW-ARE-YOU-?"
cSeparator = "-"
do while len(cString)>0
if cSeparator $ cString
cWord = left(cString,at("-",cString)-len(cSeparator))
cString = substr(cString,at("-",cString)+len(cSeparator))
else
cWord = cString
cString = ""
endif
?"WORD: "+cWord
enddo
 
My normal way of splitting strings uses two UDFs like this:

cString = "HOW-ARE-YOU-?"
cSeparator = "-"
DO WHILE NOT EMPTY(cString)
    cWord = ATXLEFT(cString, cSeparator)
    cString = ATXRIGHT(cString, cSeparator)
    ?cWord
ENDDO
    {Prints "HOW", then "ARE", then "YOU", then "?"}

See FAQ182-5975 for the code for these UDFs.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top