Home   Archive   Permalink

Useful funtion to remove the last character of a string.

I couldn't figure out how to remove the last character of a string, so my father wrote this cool function.
trim-last: func ['Removes last value' thing [string! block!] {Block or String to remove last value}][
     thing: tail thing
     thing: back thing
     remove thing
     thing: head thing

posted by:   Andrew       27-Apr-2017/20:10:07-7:00

Take/last series
Does this for you.

posted by:   Graham       27-Apr-2017/20:25:51-7:00

Another way which doesn't need a function, and a variable to hold the intermediate result:
>> series: copy [ 1 2 3 4 ]
== [1 2 3 4]
>> head remove back tail series
== [1 2 3]

posted by:   Graham       27-Apr-2017/22:35:31-7:00

If you do it this way, you could choose any index position to remove ('length string' allows you to choose the last character):
string: "asdfghjkl"
remove at string length? string
probe string

posted by:   Nick       30-Apr-2017/19:45:34-7:00

This is such a great example of my historical difficulty with REBOL, which I finally seem to be outgrowing. Andrew's function and Graham's function do the same thing with the same functions in the same order. Andrew's way is the way I would tend to write it, locked as I am into my third-generation way of thinking. But Graham's version uses the REBOL feature of passing data from one function to another which allows code to be a bit more dense and is not the way my instincts direct me.    
I have thought about writing a little monograph, for personal use, with a title along the lines of "Good Code or Bad?" where I collect examples of one way of coding something and contrast it with the "REBOL way." I suspect that if I would go back through all I have written I would find quite a few examples.

posted by:   Steven White       1-May-2017/9:14:50-7:00

I prefer the concise form, but I get the sense that I might be in the minority.

posted by:   Nick       1-May-2017/11:59:29-7:00

Steven—I had a pass at explaining this as my first entry in SO Documentation:

posted by:   Chris       1-May-2017/13:38:04-7:00

If you want economy and beauty in your script you need to factor ruthlessly. Remove the chaff to expose the bare essentials of what you want to achieve.

posted by:   Graham       11-May-2017/22:18:13-7:00