cStringOriginal text where the search will be performed.
AdvPL GuideStrTran( <cString>, <cSearch>, [cReplace], [nStart], [nCount] ) --> cResultReplaces occurrences of a character sequence inside a string.
StrTran() locates a character sequence inside another string and replaces it with new content. In daily AdvPL work, it is very common for data normalization: removing punctuation from tax IDs, phone numbers, imported codes and texts received from integrations. When the replacement content is omitted, the matched occurrence is removed.
StrTran( <cString>, <cSearch>, [cReplace], [nStart], [nCount] ) --> cResultcStringOriginal text where the search will be performed.
cSearchCharacter sequence to search for inside the original text.
cReplaceText that replaces each located occurrence. When omitted, the sequence is removed.
Default:""nStartInitial position from which replacement should start. Use it when only part of the text should be processed.
nCountMaximum number of occurrences to replace. Use it to limit the replacement scope.
User Function ExStrTranTaxId()
Local cTaxId := "12.345.678/0001-90"
cTaxId := StrTran(cTaxId, ".", "")
cTaxId := StrTran(cTaxId, "/", "")
cTaxId := StrTran(cTaxId, "-", "")
MsgInfo("Clean tax ID: " + cTaxId)
ReturnRemoves dot, slash and hyphen, returning the identifier with digits only: 12345678000190.
User Function ExStrTranSep()
Local cLine := "CODE;CUSTOMER;AMOUNT"
cLine := StrTran(cLine, ";", ",")
MsgInfo("Adjusted line: " + cLine)
ReturnReplaces semicolons with commas, producing CODE,CUSTOMER,AMOUNT.
User Function ExStrTranPhone()
Local cPhone := "(11) 99999-1234"
cPhone := StrTran(cPhone, "(", "")
cPhone := StrTran(cPhone, ")", "")
cPhone := StrTran(cPhone, " ", "")
cPhone := StrTran(cPhone, "-", "")
MsgInfo("Normalized phone: " + cPhone)
ReturnRemoves visual characters and keeps the number in a suitable format for comparison, integration or standardized storage.
© 2026 Usina.BR. All rights reserved. TOTVS, Protheus, Microsiga and their respective logos are trademarks of their owners. AdvPL Guide is independent and uses limited, attributed excerpts justified by educational purposes. See the Terms of Use. Terms of Use
Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.