Skip to content
⌘ NSIS Forum Archive

Removing \r and \n from strings

4 posts

Koen van de Sande#

Removing \r and \n from strings

I use the FileRead function to read a string from a file... but at the end, it contains trailing \r and \n characters.
What is the easiest way to remove them (using the minimum number of variables required)?
eccles#
From NSIS documentation:
;------------------------------------------------------------------------------
; TrimNewlines
; input, top of stack (i.e. whatever$\r$\n)
; output, top of stack (replaces, with i.e. whatever)
; modifies no other variables.
;
Function TrimNewlines
Exch $0
Push $1
Push $2
StrCpy $1 0
loop:
IntOp $1 $1 - 1
StrCpy $2 $0 1 $1
StrCmp $2 "$\r" loop
StrCmp $2 "$\n" loop
IntOp $1 $1 + 1

StrCpy $0 $0 $1
Pop $2
Pop $1
Exch $0
FunctionEnd
Or maybe this:
; TrimNewlines
; Usage:
; Push <string>{\r|\n}
; Call TrimNewlines
; Pop <string>
Function TrimNewlines
Exch $0
Push $1
Goto Start
Loop:
StrCpy $0 $0 -1 ; Remove last char
Start:
StrCpy $1 $0 "" -1 ; Examine last char
StrCmp $1 "$\r" Loop
StrCmp $1 "$\n" Loop
Pop $1
Exch $0
FunctionEnd
--
Dave Laundon