Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Best way to remove the last character from a string?

 [1/12] from: pwawood:mango:my at: 14-Dec-2004 20:24


I've worked out two ways to chop-off the last character of a string. Method One
>> mystring: "123456"
== "123456"
>> mystring: head remove back tail mystring
== "12345" Method Two
>> mystring: "123456"
== "123456"
>> mystring: copy/part mystring ((length? mystring) - 1)
== "12345" Can anyone suggest a less verbose, clearer and quicker way for me? Thanks Peter

 [2/12] from: lp::legoff::free::fr at: 14-Dec-2004 14:29


Hello Peter,
>>mystring: "123456"
== "123456" remove find mystring last mystring == "" probe mystring == "12345" maybe other ways. ==Philippe Selon PeterWAWood <[pwawood--mango--net--my]>:

 [3/12] from: antonr:lexicon at: 15-Dec-2004 0:47


These compete with Method 2:
>> copy/part mystring ((length? mystring) - 1)
== "12345"
>> copy/part mystring skip tail mystring -1
== "12345"
>> copy/part mystring at tail mystring -1
== "12345" to save you five characters. :) I admit this always makes me think there must be a better way, apart from making your own function. Anton.

 [4/12] from: ingo:2b1 at: 14-Dec-2004 23:57


Hi Philippe, [lp--legoff--free--fr] wrote:
> Hello Peter, >>>mystring: "123456"
<<quoted lines omitted: 3>>
> probe mystring > == "12345"
But it only works, if the last charater is nowhere else in the string.
>> mystring: "123456543"
== "123456543"
>> remove find mystring last mystring
== "456543"
>> mystring
== "12456543" This is propably not what you want. Ingo

 [5/12] from: greggirwin:mindspring at: 14-Dec-2004 10:40


Peter et al, P> I've worked out two ways to chop-off the last character of a string. .... P> Can anyone suggest a less verbose, clearer and quicker way for me? This is a great topic for general discussion of a CHOP mezzanine! I'm sure a lot of us have them, and different ones for different uses. If we can come up with a good one, then we can propose it to RT for inclusion in REBOL. The trick is that it must be a good, flexible function (IMO). Thoughts: Chop n items from head, tail, or both (default: tail)? Chop at an arbitrary position in the series, or just use AT when you make the call? n can be a number other than one, but default behavior is to chop one item? Should the series be copied, or altered in place? Return the item(s) removed, or should that be a different function (PULL, CUT)? -- Gregg

 [6/12] from: carl::cybercraft::co::nz at: 18-Dec-2004 1:08


On Tuesday, 14-December-2004 at 20:24:42 you wrote,
>I've worked out two ways to chop-off the last character of a string. > >Method One > >>> mystring: "123456" >== "123456" >>> mystring: head remove back tail mystring >== "12345"
Note that REMOVE works directly on the string, so re-defining mystring and HEAD isn't needed there...
>> mystring: "123456"
== "123456"
>> remove back tail mystring
== ""
>> mystring
== "12345" And a fun alternative...
>> mystring: "123456"
== "123456"
>> mystring: head reverse remove head reverse mystring
== "12345" :-) -- Carl Read.

 [7/12] from: SunandaDH:aol at: 15-Dec-2004 4:01


Gregg:
> This is a great topic for general discussion of a CHOP mezzanine!
Let me remind you of some of your earlier work. http://www.rebol.org/cgi-bin/cgiwrap/rebol/ml-display-message.r?m=rmlQNWK It could easily be recast as chop/left /right /middle etc Sunanda

 [8/12] from: luc::spirlet::skynet::be at: 15-Dec-2004 19:50


Other fun alternative mystring: "abcdef" reverse mystring remove mystring reverse mystring result: "abcde" If you define mystring: "aaaaaa" The last a is removed. It's sure. But nobody don't prove it. It works too. :-)) Luc Spirlet Carl Read wrote:

 [9/12] from: greggirwin:mindspring at: 15-Dec-2004 11:52


Hi Sunanda, Sac> Let me remind you of some of your earlier work. Ahh, back when I was a newbie. :) -- Gregg

 [10/12] from: pwawood::mango::net::my at: 20-Dec-2004 12:09


Sunanda Looks a little too Reverse Polish for me!! Peter On Tuesday, Dec 14, 2004, at 21:38 Asia/Kuala_Lumpur, [SunandaDH--aol--com] wrote:

 [11/12] from: cyphre:seznam:cz at: 15-Dec-2004 8:21


Hi all, I made simple performace measurement of a few methods with the same effect: --------------<begin code>-------------- profile: func [ blk n /local st ][ st: now/time/precise loop n blk print [n "cycles took" now/time/precise - st] ] methods: [ [mystring: "123456" mystring: head remove back tail mystring] [mystring: "123456" mystring: copy/part mystring ((length? mystring) - 1)] [mystring: "123456" copy/part mystring ((length? mystring) - 1)] [mystring: "123456" copy/part mystring skip tail mystring -1] [mystring: "123456" copy/part mystring at tail mystring -1] [mystring: "123456" mystring: head reverse remove head reverse mystring] [mystring: "123456" mystring: head remove at mystring length? mystring] ] foreach m methods [ print ["measuring method:" mold m] profile m 10'000'000 ] --------------<end code>-------------- results on my machine: measuring method: [mystring: "123456" mystring: head remove back tail mystring] 10000000 cycles took 0:00:11.953 measuring method: [mystring: "123456" mystring: copy/part mystring ((length? mystring) - 1)] 10000000 cycles took 0:00:17.407 measuring method: [mystring: "123456" copy/part mystring ((length? mystring) - 1)] 10000000 cycles took 0:00:16.406 measuring method: [mystring: "123456" copy/part mystring skip tail mystring -1] 10000000 cycles took 0:00:16.828 measuring method: [mystring: "123456" copy/part mystring at tail mystring -1] 10000000 cycles took 0:00:16.86 measuring method: [mystring: "123456" mystring: head reverse remove head reverse mystring] 10000000 cycles took 0:00:23.219 measuring method: [mystring: "123456" mystring: head remove at mystring length? mystring] 10000000 cycles took 0:00:14.187 ...so it looks 'head remove back tail mystring' is the fastest one for now. feel free to add your different methods and post results here! regards, Cyphre

 [12/12] from: ptretter:charter at: 15-Dec-2004 14:21


I say just add /tail to 'copy and 'remove Paul Tretter

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted