xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

StrTran()

Searches and replaces characters within a character string or memo field.

Syntax

StrTran( <cString>   , ;
         <cSubString>, ;
        [<cReplace>] , ;
        [<nStart>]   , ;
        [<nCount>]     ) --> cNewString

Arguments

<cString>
This parameter is the input string or memo field to search <cSubString> in.
<cSubString>
This is the character string to search for in <cString>.
<cReplace>
A character string <cSubString> is replaced with in <cString>. It defaults to an empty string (""), i.e. if <cReplace> is not specified, <cSubString> is removed from <cString>.
<nStart>
This is a numeric value indicating the first occurrence of <cSubString> to replace. The default value is 1.
<nCount>
This is a numeric value indicating the number of occurrences of <cSubString> to replace. If not specified, all occurrences of <cSubString> are replaced.

Return

The function returns a copy of <String> where <cSubString> is replaced with <cReplace>.

Description

StrTran() is a powerful search and replace function used to modify character strings. The function searches <cSubString> in the input string and replaces it with the replacement string. If <nStart> and <nCount> are not specified, all occurrances of <cSubString> are replaced. The search and replacement strings do not need to have the same length.

Info

See also:At(), HardCR(), HB_RegExReplace(), MemoTran(), RAt(), StrDel(), Stuff(), SubStr()
Category: Character functions
Source:rtl\strtran.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example shows various possibilities for StrTran() usage.
// It begins with simple character search and replacements. At the
// end, StrTran() is used to create a macro expression that creates
// an array from a character string.

   PROCEDURE Main
      LOCAL cString := "a,BBB,cccc"
      LOCAL aArray

      ? StrTran( cString, "B" )              // result: a,,cccc

      ? StrTran( cString, "B", "b", 2 )      // result: a,Bbb,cccc

      ? StrTran( cString, "c", "C", 2 , 2 )  // result: a,BBB,cCCc

      aArray := &( '{"' + StrTran( cString, ',', '","' ) + '"}' )

      ? aArray[1]                            // result: a
      ? aArray[2]                            // result: BBB
      ? aArray[3]                            // result: cccc
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe