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

Is there a way to separate characters?

Status
Not open for further replies.

buddyrich2

Technical User
Apr 12, 2006
87
US
I have a (paper)form to fill out that requires me to put each character in a separate box (i.e. a first name). I was thinking I would take a string and separate out each character, put each character into a temporary table, then use that to put the characters in in the appropriate place on the form.

Is this possible and if so, can someone give me an example of the code?

Thanks!
 
How about this:
Code:
lcStr = [MyFirstName]
WAIT WINDOW TRANSFORM(lcStr,[@R X  X  X  X  X  X  X  X  X  X  X])

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
BuddyRich2
In case Borislav's suggestion will not work, Here
One routine to break it apart and one to put it back
I assumed you had the table already created.

Did not know the life span of the table so I did not resuse the same record. I assumed you would Zap it at least once a day.

Code not tested, you will have to do that.

Code:
PROCEDURE BREAKAPART
LPARAMETERS tcString
tcString = ALLTRIM(UPPER(tcString))
lcLen = LEN(tcString)
lcAlias=ALIAS()

IF USED(TEMPTABLE)
	SELECT TEMPTABLE
ELSE
	SELECT 0
	USE TEMPTABLE
ENDIF

APPEND BLANK

FOR x = 1 TO lcLen
	lcChar = SUBSTR(lcString,x,1)
	lcField=FIELD(x)
	replace (lcField) WITH lcChar
ENDIF

lnRecno = RECNO()
USE
SELECT (lcAlias)

RETURN lnRecno


PROCEDURE PUTITBACK
LPARAMETERS tnRecno
lcAlias=ALIAS()

IF USED(TEMPTABLE)
	SELECT TEMPTABLE
ELSE
	SELECT 0
	USE TEMPTABLE
ENDIF

GOTO tnRecno

FOR x = 1 TO 255
	lcChar = EVALUATE(FIELD(x))
	lcString = lcString + lcChar
ENDFOR

USE
SELECT (lcAlias)

RETURN lcString

David W. Grewe Dave
 
Depending on which version of VFP you are using, it is easy to chop up a string into it's component parts. All you need to do is to add a separator and the use GETWORDCOUNT() and GETWORDNUM() to retrieve the elements.

This little function returns a string with the specified separator between every set of characters (default is a comma between every 1 character). I use this one a lot when validating text!

FUNCTION PadString( tcInString, tcPadChar, tnChars )
LOCAL lnLen, lcOutStr, lcPadChar

*** Default pad character to a comma if not passed
lcPadChar = IIF( VARTYPE( tcPadChar ) # "C" OR EMPTY(tcPadChar), ",", tcPadChar)

*** Default to single characters if nothing passed
lnChars = IIF( VARTYPE( tnCHars ) # "N" OR EMPTY( tnChars ), 1, tnChars )

*** Intersperse each character set with the pad character
lnLen = LEN( ALLTRIM( tcInstring ))
lcOutStr = ''
FOR lnCnt = 1 TO lnLen STEP lnChars
lcOutStr = lcOutStr + SUBSTR( tcInstring, lnCnt, lnChars ) + lcPadChar
NEXT
*** Return the result
RETURN lcOutStr

Then you can use code like this to get each block of data that you created above:

lcPadStr = PadString( "SourceData", "-")
lnWords = GETWORDCOUNT( lcPadStr, "-" )
FOR lnCnt = 1 TO lnWords
lcNextVal = GetWordNum( lcPadstr, lnCnt, "-"
NEXT

NOTE: In early versions of VFP (and Fox 2.x) these functions were shipped in FoxTools.FLL but they were called "WORDS()" and "WORDNUM()" respectively.


----
Andy Kramek
Visual FoxPro MVP
 
AndyKr... could you show me an example of text you used this code against? Looks interesting... but well, curious.
 
>>could you show me an example of text you used this code against

Sure. One of my clients deals with data that contains a set of two-letter codes in a single data field. (These are standardized "Amenity Codes" relating to property). However, sometimes these are entered with spaces between the codes, other times with hyphens and occasionally with commas.
To processing the data we insert a pipe "|" between the three-character blocks and then use that to retrieve each code with its terminator (which we then strip off) and save them off.
So a string like this (note the last code is only TWO characters...):
AA,BB FR-GR/HH
is transformed by Padstring( 'AA,BB FR-GR/HH', "|, 3 )
AA,|BB |FR-|GR/|HH|
Which we then extract using GETWRODCOUNT() and GETWORDNUM()
and turn into single codes (one record per code) like this
AA
BB
FR
GR
HH


----
Andy Kramek
Visual FoxPro MVP
 
Here's yet another way to do it:

Code:
x = "ABCDE"
? STRTRAN(STRCONV(x, 5), CHR(0), " ")
    && outputs "A B C D E "

This is no better or worse than the other suggested methods. I'm just throwing it in to demonstrate the versatility of these functions.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
After the letters are seperated by a space, you can get the single letters to an array. As long as it's latin letters, Mike's solution is just fine:

Code:
Local lcText, lcSeparated, lnCount
lcText = "OLAF DOSCHKE"
lcSeparated = STRTRAN(STRCONV(lcText, 5), CHR(0), " ")
Local Array laLetters[1]
For lnCount=1 TO ALINES(laLetters,lcSeparated," ")
   ? laLetters[lnCount]
Endfor lnCount

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top