Post Reply 
Best Way to Strip Traling Letter?
03-20-2017, 05:52 PM
Post: #1
Best Way to Strip Traling Letter?
In a program I'm writing, I want to strip the trailing "S" from a word. Since I'm a BASIC programmer, I came up with the following code. It works but I'd like to know if there's a better way to do it on the Prime - a more "Prime-fluent" way, if you will.

Code:
T:=U;
IF RIGHT(T,1)=="S" THEN
  T:=LEFT(T,DIM(T)-1);
END;

Thanks for any advice!

Tom L
Find all posts by this user
Quote this message in a reply
03-20-2017, 08:54 PM (This post was last modified: 03-20-2017 09:09 PM by Didier Lachieze.)
Post: #2
RE: Best Way to Strip Traling Letter?
Another way to do it :
Code:
T:=U(1,DIM(U)-(RIGHT(U,1)=="S"))
Find all posts by this user
Quote this message in a reply
03-20-2017, 09:49 PM (This post was last modified: 03-20-2017 09:50 PM by Carlos295pz.)
Post: #3
RE: Best Way to Strip Traling Letter?
For "S" and "s" it may is this:
Code:
T:=U(1,DIM(U)-(INSTRING("Ss",RIGHT(U,1))>0))

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
03-20-2017, 10:05 PM
Post: #4
RE: Best Way to Strip Traling Letter?
(03-20-2017 09:49 PM)Carlos295pz Wrote:  For "S" and "s" it may is this:
Code:
T:=U(1,DIM(U)-(INSTRING("Ss",RIGHT(U,1))>0))

Or:
Code:
T:=U(1,DIM(U)-(UPPER(RIGHT(U,1))=="S"))
Find all posts by this user
Quote this message in a reply
03-20-2017, 11:05 PM
Post: #5
RE: Best Way to Strip Traling Letter?
As in example
Code:

suppress("depends",DIM("depends"))
Find all posts by this user
Quote this message in a reply
03-21-2017, 01:33 AM
Post: #6
RE: Best Way to Strip Traling Letter?
(03-20-2017 11:05 PM)toshk Wrote:  As in example
Code:

suppress("depends",DIM("depends"))

Under speed test, this form takes approximately 15 times more time of execution than the initial one. It is an alternative, but not massively adequate.

Code:
T:=LEFT(T,DIM(T)-1)

The RIGHT LEFT INSTRING and similar commands are incredibly fast

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
03-21-2017, 03:07 AM (This post was last modified: 03-21-2017 03:07 AM by toml_12953.)
Post: #7
RE: Best Way to Strip Traling Letter?
(03-20-2017 08:54 PM)Didier Lachieze Wrote:  Another way to do it :
Code:
T:=U(1,DIM(U)-(RIGHT(U,1)=="S"))

I like it! It's elegant and slightly obscure depending on the fact that a test for equality returns 1 if true. I wasn't aware you could extract substrings using an indexing scheme like that without using string functions (it's the same method used in ISO Full BASIC). This will simplify my programs considerably. Thanks!

Tom L
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)