Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Search for character and replace it
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00492710
Message ID:
00495965
Views:
13
This is an article from LES Printer that I'm translating to Spanish:
"
I often need to help users clean up their data. One of the things that they need is a way to change a word or phrase and replace it with another one.
This utility function does just that. By using just a few parameters, you can give your users a way to do the cleanup themselves.
The utility has three required parameters, and one optional one. You have to give it the name of the field in which to look for the offending string, the string to look for, and what to replace it with.
In addition, if you want your search to find matches without regard to whether they’re uppercase or lowercase, setting the fourth parameter to .T. will find everything that matches, regardless of case.
Here’s the code:


PARAMETERS pField, pString, pReplacement, CaseInsensitive
IF EMPTY ( ALIAS() )
MessageBox ( [No table is open in the current work area] )
RETURN
ENDIF
IF VarType ( &pField ) = [U]
MessageBox ( [Table ] + ALIAS() :
+ [ doesn’t contain a field named "] + pField + ["] )
RETURN
ENDIF
IF NOT CaseInsensitive
LOCATE FOR pString $ &pField
ELSE
LOCATE FOR UPPER ( pString ) $ UPPER ( &pField )
ENDIF
IF NOT FOUND()
Msg = [No occurence found of "] + pString + [" in field "] + pField + [" ]
Msg = Msg + [(case ] + IIF (NOT CaseInsensitive, [], [in]) + [sensitive)]
MessageBox ( Msg, 64, [Data Search and Replace] )
RETURN
ENDIF

IF NOT CaseInsensitive
SCAN REST FOR pString $ &pField
Loc = AT ( pString, &pField )
IF Loc = 1
NewString = pReplacement + SUBS( &pField, LEN(pString) +1 )
ELSE
NewString = LEFT( &pField,Loc-1 ) ;
+ pReplacement + SUBS( &pField, Loc + LEN(pString) )
ENDIF
REPLACE &pField WITH NewString
ENDSCAN
ELSE
* Use a Proper case replacement if the original was Proper case
SCAN REST FOR UPPER(pString) $ UPPER(&pField)
Loc = AT ( UPPER(pString), UPPER(&pField) )
pPReplacement = IIF ( IsUpper(SUBSTR(&pField,Loc,1)), ;
PROPER(pReplacement), pReplacement )
IF Loc = 1
NewString = pPReplacement + SUBS( &pField, LEN(pString) +1 )
ELSE
NewString = LEFT( &pField,Loc-1 ) ;
+ pPReplacement + SUBS( &pField, Loc + LEN(pString) )
ENDIF
REPLACE &pField WITH NewString
ENDSCAN
ENDIF

How it works:
The function first verifies that a table is open in the current work area, and that a valid field name has been passed. It then looks for the first record containing a match on the target string; if there isn’t at least one match, it displays a message to that effect and gives up.
If there’s at least one match, it scans the rest of the records (SCAN REST starts wherever the record pointer is, saving a repeat of the initial search) and replaces each field containing a match with
· Everything to the left of the target string,
· The replacement string itself, and
· The remainder of the string.
The only "gotchas" are that (1) the target string might match with the first character, and (2) If the target is proper case, we want its replacement to be proper case as well. If this feature isn’t appropriate for your data, take it out.

"

Hope this helps
B.D.H.
Previous
Reply
Map
View

Click here to load this message in the networking platform