xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

PosChar()

Replaces a single character at a specified position in a string.

Syntax

PosChar( <cString>, <xChar>, [<nPos>] ) --> cResult

Arguments

<cString>
This is the character string to replace a single character in.
<xChar>
The replacment character can be specified as a character or its numeric ASCII code.
<nPos>
Optionally, the position of the character to replace can be specified as a numeric value. By default, <nPos> specifies the last character (Len(<cString>))

Return

The function substitues the character at the specified position and returns the result. If CSetRef() is set to .T. (true), pass <cString> be reference to avoid the return value.

Note:  the function exists for compatibility reasons. The [ ] operator can be applied to character strings in xHarbour and is more efficient for replacing a single character.

Info

See also:[ ] (string), CharRepl(), CSetRef(), Stuff()
Category: CT:String manipulation , Character functions
Source:ct\pos2.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example shows different ways of replacing a
// single character in a string.

   PROCEDURE Main
      LOCAL cString := "xHarbour"

      // compatibility
      CSetRef( .F. )
      ? PosChar( cString, "Y" )     // result: xHarbouY
      ? PosChar( cString, "Z", 1 )  // result: ZHarbour
      ? cString                     // result: xHarbour

      CSetRef( .T. )
      ? PosChar( @cString, "Y" )    // result: NIL
      ? PosChar( @cString, "Z", 1 ) // result: NIL
      ? cString                     // result: ZHarbouY

      // xHarbour [] operator
      ? cString := "xHarbour"       // result: xHarbour
      ? cString[-1] := "Y"          // result: Y
      ? cString[ 1] := "Z"          // result: Z
      ? cString                     // result: ZHarbouY

   RETURN

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