| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Replaces a single character at a specified position in a string.
PosChar( <cString>, <xChar>, [<nPos>] ) --> cResult
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.
| See also: | [ ] (string), CharRepl(), CSetRef(), Stuff() |
| Category: | CT:String manipulation , Character functions |
| Source: | ct\pos2.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// 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
http://www.xHarbour.com