String manipulation in AdvPL
Combines text search, slicing and replacement.
Overview
AllTrim removes outer spaces. At finds the first occurrence and RAt the last; zero means absent. SubStr uses a one-based start and length, while Stuff removes a count and inserts text into a new string. Lower/Upper change case; Asc/Chr map character codes. Len measures representation length rather than a universal Unicode character count. This example uses fixed ASCII inputs. (Sources: refs. 1–10.)
Commented example
#Include "Protheus.ch"
User Function Ed409()
Local cText := " AB-CD-AB "
// Normalize before calculating positions.
cText := AllTrim(cText)
ConOut(cValToChar(Len(cText)))
ConOut(cValToChar(At("-",cText)))
ConOut(cValToChar(RAt("-",cText)))
ConOut(SubStr(cText,4,2))
ConOut(Stuff(cText,4,2,"XY"))
ConOut(Lower(cText))
ConOut(Upper(Lower(cText)))
ConOut(cValToChar(Asc("A")))
ConOut(Chr(65))
Return Nil8; 3; 6; CD; AB-XY-AB; ab-cd-ab; AB-CD-AB; 65; A. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Check absence before using a position; define input encoding.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.