Archive: delete last char in string if char is...


delete last char in string if char is...
Hello!
(first: sry for my english)

i want to have an function that does the following:

input is a string. if the string ends on a special char (the other input), the char will be deleted. otherwise nothing should be done. sounds trivial, but i don't get by with the push's and pop's etc really well. so i hope somebody already got that function or can write it in a few minutes, because it would take too much time for me, to write it by myself. i think it shouldn't be a problem to the most of the forum-members. thank you =)


PS.: i looked for that function in the archive, but didn't found what i want. Important is, that the char will only be deleted, if it's the last of the string. if the char exists in the string(n-times), but is not at last position nothing should be done.


;Push "a"
;Push "blahaha"
;Call ChopLastChar
;Pop $R0
;$R0 == blahah

Function ChopLastChar
Exch $R0
Exch
Exch $R1
Push $R2
StrCpy $R2 $R0 1 -1
StrCmp $R2 $R1 0 +2
StrCpy $R0 $R0 -1
Pop $R2
Pop $R1
Exch $R0
FunctionEnd


-Stu

Wow!
That was really fast. Thank you very much.


Wow, this topic is really old... sorry to revive it, but I think my problem is too similar to create a separate one.

I need to delete backslash from string:
"$INSTDIR\blablabla\someting\" --> "$INSTDIR\blablabla\someting"

The problem I have to deal with is so common, that I began to wonder maybe there is a built-in function or something? I mean... if nothing else, I can use the solution from Stu, but it looks like some magic to me and I prefer my code as short and clear as possible. Clear to me, to be precise :)


StrCpy $0 "a string" "" -1 # = "g"
StrCmp $0 "g" 0 +2
StrCpy $0 "a string" -1 # = "a strin"


Put the code to call my function in a macro or just use the 3 main lines inside it (which is the same as what bnicer has posted). The reason for the function call with the rest of it is to stop variables being clobbered.

Stu


Thanks for quick replies guys. 3 lines are something I can analyze :D

So far I managed to shorten the code even more to:

StrCpy $OutputString $InputString -1

I don't need to save that last char anywhere, or even check if it's really a backslash, so code above does the trick.

Ah, also output can be the same as input, so to trim last char from $INSTDIR I can use:
StrCpy $INSTDIR $INSTDIR -1


By accident I found something else. When reading a path from from registry using ReadRegStr it works like this:
RegValue == "X:\path" --> ReadRegStr == "X:\path"
RegValue == "X:\path\" --> ReadRegStr == "X:\path"
In short words if there is backslash in the end, it is ommited... meaning I didn't need the code in the first place. Don't I feel dumb now not to test that earlier...

ReadRegStr will read the exact value, it will not remove any backslashes.

It is actually the $INSTDIR variable that removed your backslash. $INSTDIR is a special variable, it is actively maintained by NSIS to always contain a valid path. Trailing backslashes, spaces, etc are automatically removed after its value is changed by any command.


Is the $INSTDIR the only variable to perform this trick? Or are there others to do this?


There are others, [r]0-9 are normal and language, hwndparent and path variables are special