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

[REBOL] Re: Alternate TRIM need

From: carl::cybercraft::co::nz at: 25-Aug-2004 23:01

>=== Original Message > > >Jim: > >> I need to trim a string's delimiters. Is there a built in function for >> this? >> >> Or does someone have a nice function? > > PhoneString: {"123-4567"} > print trim/with phonestring {"} > 123-4567 > >trim/with will remove *all* "s not just the first and last: > > PhoneString: {"1"2"3"-4"567"} > print trim/with phonestring {"} > 123-4567 > >Good enough? >Sunanda
If not, then perhaps something like this... trim-delims: func [str [string!] delims[char! string!]][ if not empty? str [ delims: join delims/1 last delims if delims/2 = last str [remove back tail str] if (to-string delims/1) = copy/part str 1 [remove str] ] str ] It accepts one character for when the start and end deliminaters are the same and two for when they're different. in use...
>> trim-delims "" {"}
== ""
>> trim-delims "a" {"}
== "a"
>> trim-delims {"a"} {"}
== "a"
>> trim-delims "{a}" {"}
== "{a}"
>> trim-delims "{a}" "{}"
== "a"
>> trim-delims "<abc>" "<>"
== "abc" then again, perhaps LOAD does all you need? ...
>> load {"123-4567"}
== "123-4567" Hmmm. :-) -- Carl Read