Function StrTran() Foundation

Replaces characters in a character string or memo field.

Syntax
StrTran( <cString>, <cSeek>, ;
        [<cReplace>], [<nStart>], [<nCount>] ) --> cNewString
Parameters
<cString>
<cString> is a character string or a memo field in which characters are replaced.
<cSeek>
<cSeek> are the characters which are replaced.
<cReplace>
<cReplace> is the string which replaces <cSeek>. If this argument is missing, the characters <cSeek> in <cString> are replaced by a null string ("").
<nStart>
<nStart> is a numeric value specifying that the replacement in <cString> is to begin from the <nStart> occurrence of <cSeek>. By default the replacement is begun with the first <cSeek>.
<nCount>
<nCount> is a numeric value specifying how many times <cSeek>is replaced. If this argument is missing, all occurrences of <cSeek> are replaced.
Return

The return value of StrTran() is a character string in which <cSeek> is replaced with <cReplace> from the position <nStart> exactly <nCount> times.

Description

The character function StrTran() searches a character string for a character or a string and replaces occurrences of this string with another character or string. When values are not specified for <nStart> and <nCount>, all instances of <cSeek> found are replaced with <cReplace>. When <cReplace> is not specified, <cSeek> is replaced with a null string (""), causing <cSeek>to be deleted from <cString>.

StrTran() replaces all occurrences of <cSeek>. To replace only a single occurrence of <cSeek>, the function Stuff() is used. For memo fields or character strings with control characters, the functions HardCR() and MemoTran() are used to replace carriage returns.

Examples
StrTran()
// The example shows various results of the function StrTran() 

PROCEDURE Main 

   ? StrTran("Abc;Abc;Abc", "Abc" ) 
                                    // result: ;; 
   ? StrTran("Abc;Abc;Abc", "Abc", "Def" ) 
                                    // result: Def;Def;Def 
   ? StrTran("Abc;Abc;Abc", "Abc", "Def", 2 ) 
                                    // result: Abc;Def;Def 
   ? StrTran("Abc;Abc;Abc", "Abc", "Def", 1, 1 ) 
                                    // result: Def;Abc;Abc 

RETURN 

Convert color list into an array

// In this example, StrTran() and the macro operator 
// are used to convert the return value of SetColor() 
// to an array which contains an individual color 
// value with foreground and background color 
// in each of its elements. 

PROCEDURE Main 
   LOCAL aColor := ColorToArray( SetColor() ) 

   AEval( aColor, {|cColor| QOut( cColor ) } ) 

RETURN 

FUNCTION ColorToArray( cColor ) 

   cColor := '{"' + StrTran( cColor, ',' , '","' ) + '"}' 

RETURN &(cColor) 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.