ChrTran(), StrTran(), Stuff(), Sys(15), Sys(20)
These five methods of manipulating strings are great for translation and some cool effects.
Usage |
cRetVal = CHRTRAN( cSource, cCharsToReplace, cReplacements ) cRetVal = STRTRAN( cSource, cStringToReplace [, cReplacementString [, nStartOccurrence [, nHowMany [, nFlags ] ] ] ]) cRetVal = STUFF( cSource, nWhereToStart, nLong, cReplacementString ) cRetVal = SYS( 15, cSource, cTransTable ) cRetVal = SYS( 20, cSource, nLength ) |
Parameter |
Value |
Meaning |
cSource |
Character |
The original string to be manipulated. |
cCharsToReplace |
Character |
The individual characters to be replaced. |
cReplacements |
Character |
The new characters to replace those of cCharsToReplace, mapped one-to-one to the originals. |
cStringToReplace |
Character |
The exact string to locate and replace in cSource. |
cReplacementString |
Character |
The new string to be substituted for cStringToReplace. |
Omitted |
cStringToReplace is cut from the text. |
|
nStartOccurrence |
Numeric |
Optionally specify at which occurrence the replacement starts. |
Omitted |
Replacement starts at the first occurrence. |
|
nHowMany |
Numeric |
How many of the occurrences to replace. |
Omitted |
All occurrences are replaced. |
|
nFlags |
0 or 2 |
Perform a case-sensitive search. If cStringToReplace is found, replace it with cReplacementString in its current case. |
1 |
Perform a case-insensitive search. If cStringToReplace is found, replace it with cReplacementString in its current case. |
|
3 |
Perform a case-insensitive search. If cStringToReplace is found, replace it with cReplacementString, matching the case of cStringToReplace as much as possible. |
|
nWhereToStart |
Numeric |
Starting location for string replacement. |
nLong |
Numeric |
Number of characters to be discarded from the original string. |
0 |
Preserve all existing characters while inserting new text. |
|
cTransTable |
Character |
A string of up to 255 characters; replaces original characters based on position in string and ASCII value of original. |
nLength |
Numeric |
The number of characters of cSource to process. |
Example |
? CHRTRAN("ABCDE", "ACE", "XYZ") && yields "XBYDZ" ? CHRTRAN("ABCDE", "ACE", "") && yields "BD" ? CHRTRAN("ABCDE", "ACE", "X") && yields "XBD" ? STRTRAN("The brown fox", "brown", "red") && "The red fox" ? STRTRAN("1a2a3a4a5a6a7a", 'a', 'b', 3, 2) && "1a2a3b4b5a6a7a" ? STRTRAN("RED, Red, red", "red", "blue") && yields "RED, Red, blue" ? STRTRAN("RED, Red, red", "red", "blue", -1, -1, 1) && yields "blue, blue, blue" ? STRTRAN("RED, Red, red", "red", "blue", -1, -1, 3) && yields "BLUE, Blue, blue" ? STUFF("5 pound sack", 1, 7, "10 pounds of potatoes in") * results in "10 pounds of potatoes in sack" |
* Invert - return string with case reversed LPARAMETER tcString LOCAL i, cTransStr && counter and string cTransStr = "" FOR i = 1 TO 255 DO CASE CASE NOT ISALPHA(CHR(I)) && not a letter - don't change! cTransStr = cTransStr + CHR(i) CASE ISLOWER(CHR(I)) && lower case cTransStr = cTransStr + UPPER(CHR(i)) CASE ISUPPER(CHR(I)) && upper case cTransStr = cTransStr + LOWER(CHR(i)) ENDCASE NEXT RETURN SYS(15,cTransStr,tcString)Applying this function to any block of text returns the original string, but with all uppercase characters changed to lower and all lowercase changed to upper.
See Also |
AllTrim(), ChrTranC(), Chr(), Left(), LTrim(), PadC(), PadL(), PadR(), Replicate(), Right(), Set Collate, StuffC(), SubStr(), Trim() |