replace last character

Started by change last text on 8-Dec-2016/22:23:06-8:00
How do I replace the last character of a string ? e.g if I have c: 'abcdef' and I want to change the last character to 'z' so that c becomes 'abcdez' I was trying change/part c 'z' length? c it did not work.
str/(length? str): "z" That's clunky, but works.
Another way would be: change back tail c "z"
Another way: poke c length? c #"z"
BTW, your original way also works, if you use double quotes: change/part c "z" length? c
@nick, when I use double quote it changes the whole string into z , rather than abcdez. pls see below c: "abcdef" == "abcdef" >> change/part c "z" length? c == "" >> c == "z"
@sunanda, getting an error when trying str/(length? str): "z" >> c: "abcdef" == "abcdef" >> c/(length? c): "z" ** Script Error: Invalid argument: z ** Where: halt-view ** Near: c/(length? c): "z" >>
@nick - poke c length? c #"z" works. >> c: "abcdef" == "abcdef" >> poke c length? c #"z" == #"z" >> c == "abcdez" >> To all of you, thanks a lot for helping out.
Sunanda's example works if you change the "z" string to a #"z" character: c/(length? c): #"z"
thanks Nick. for my own learning process, what does the # mean? I did a search on the site to see what # mean, the only place I could find something is in rebolcore-16.html. is it converting to binary in c/(length? c): #"z" and poke c length? c #"z"
I also saw # being used as char in chapter 4 - http://www.rebol.com/docs/core23/rebolcore-4.html#section-3
The # symbol is used to refer to the char! datatype (a single character). The string and character datatypes are different, so if a function expects a char!, and gets a string!, you'll get an error. BTW, you can cast a char! from a single character string using: to-char "z" == #"z"

Reply