String Splitting in Foxpro Visual 9

Viewed 634

I have a column of strings separated by comma.

Example: City, Zipcode

I want to make a column with only city populated so everything before the comma.

How has anyone else accomplished this? I know with Foxpro you can usually accomplish the same task various ways. Any help would be appreciated.

EDIT: SOLUTION

GETWORDNUM(FIELD,1,",")

This worked to give the text string before the comma from the column FIELD.

3 Answers

The easiest way to do that is to use STREXTRACT(). ie:

lcColumnData = "City, Zipcode"

? STREXTRACT(m.lcColumnData, "",",")
STORE ALINES(aCZ, "Atlanta, 30301", ",") TO iCZ

City = aCZ[1]
ZipCode = aCZ[2]

?City
?ZipCode

Try this:

str = "City, Zipcode"

*initialize the column value leftcol
leftcol = ''   
*find comma position
pos = At(',', str)
Do Case
  Case pos > 1 
    * there is a comma and something before that. take everything before that pos
    leftcol = Left(str, pos-1)

  Case pos = 1
    * first char is comma
    leftcol = ''

  Otherwise
    *there is no comma. take the whole string
    leftcol = str
EndCase
Related