Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Strtran()
Message
De
08/12/2012 16:15:58
 
 
À
08/12/2012 12:53:14
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows XP
Divers
Thread ID:
01559129
Message ID:
01559136
Vues:
130
This message has been marked as the solution to the initial question of the thread.
A=STRTRAN('KHALID','ALI','123 ')
MESSAGEBOX(A)
Two solutions:
(1) (won't always work) Change it to include spaces pre- and post-:
A=STRTRAN(STRTRAN('KHALID', SPACE(1) + 'ALI','123 '), 'ALI' + SPACE(1), '123)
MESSAGEBOX(A)
(2) (should always work), though you'll have to manually handle punctuation and unusual spacing:
FUNCTION substituteWord
LPARAMETERS tcText, tcWordToSeek, tcWordNew, tlWholeWordOnly

IF tlWholeWordOnly
    * Doing a word at a time
    * Note:  If you are having punctuation or variable spacing, you'll have to handle that manually.
    lcResult = SPACE(0)
    FOR lnI = 1 to GETWORDCOUNT(tcText)
    	lcWord = GETWORDNUM(tcText, lnI)
    	IF lcWord == tcWordToSeek
	    	lcWord = STRTRAN(GETWORDNUM(tcText, lnI), tcWordToSeek, tcWordNew)
	    ENDIF
    	lcResult = lcResult + IIF(EMPTY(lcResult), SPACE(0), SPACE(1)) + lcWord
    NEXT
    * When we get here, every word was replaced
    RETURN lcResult

ELSE
	* Doing any occurrence within
	RETURN STRTRAN(tcText, tcWordToSeek, tcWordNew)
ENDIF
Usage:
? substituteWord("kalid ali kha", "ali", "123", .f.)  && Substitute NOT whole words
? substituteWord("kalid ali kha", "ali", "123", .t.)  && Substitute ONLY whole words
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform