Post Reply 
How do you delete characters in a string?
10-31-2017, 03:13 AM
Post: #1
How do you delete characters in a string?
I need to remove a few characters in a string. I've tried to do it one by one using S(3):=""; but it doesn't work. S(3):="g" does replace a character but not with the null string "". I've tried S(3,1):= but you can't assign a value to this. REPLACE will work but you need to know the string beforehand and it'll replace other occurances of this string.

The only way I can see is to take the string before and after the point I want to remove the chars, and create a new string by appending them. This seems like a lot of work for such a simple task. Anyone know how to just remove a few characters at a known point in a string?
Find all posts by this user
Quote this message in a reply
10-31-2017, 03:22 AM (This post was last modified: 10-31-2017 03:23 AM by Tim Wessman.)
Post: #2
RE: How do you delete characters in a string?
Do it the longer way or else wait a bit.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
10-31-2017, 03:29 AM
Post: #3
RE: How do you delete characters in a string?
What do you mean wait a bit?
Find all posts by this user
Quote this message in a reply
10-31-2017, 03:56 AM
Post: #4
RE: How do you delete characters in a string?
"suppress" will remove an item (cas command) but the quickest way is still going to be make your own delete command if you are trying to remove N objects starting at position M. Takes 1 line...

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
10-31-2017, 05:07 AM
Post: #5
RE: How do you delete characters in a string?
Ah, suppress works! ....as in S:=suppress(S,3);
Find all posts by this user
Quote this message in a reply
10-31-2017, 11:59 AM (This post was last modified: 10-31-2017 12:05 PM by webmasterpdx.)
Post: #6
RE: How do you delete characters in a string?
suppress() works but it's much faster to use RIGHT() and LEFT() functions. Be careful as there are CAS functions right() and left() that are related to equations.

So, the optimal way to drop characters in a string is as follows.

Code:

// Drops characters from string
// S   string
// L   Position before first character to drop
// R   Position after last character to drop
// e.g. S:=STRDEL(S,3,7); drops S(4), S(5) and S(6) from S.
EXPORT STRDEL(S,L,R)
BEGIN
RETURN LEFT(S,L)+RIGHT(S,DIM(S)-R+1);
END; // BEGIN...

I tested this a couple of different ways using TEVAL. I ran code using a string with suppress, scanning the string using FOR loops and finally using RIGHT and LEFT. I just ran the same code 10000 times to get the timing. Note that suppress was as faster than the FOR loops for dropping a single character since it's doing much the same thing except the FOR loop in suppress is in machine code. However, when you drop multiple characters calling suppress multiple times, you are effectively scanning the string over and over again, so the FOR loop catches up when you get to 5 or so characters to drop. The RIGHT and LEFT versions was always faster for everything. Dropping 5 characters using suppress was around 70 seconds while RIGHT and LEFT was only 5 seconds....considerably faster. Dropping one character, suppress was around 15 seconds while RIGHT and LEFT was around 5 seconds still.
Enjoy.
Find all posts by this user
Quote this message in a reply
10-31-2017, 12:26 PM (This post was last modified: 10-31-2017 12:26 PM by Arno K.)
Post: #7
RE: How do you delete characters in a string?
Did you try something like "SUPPRESS(s,"2357")" which deletes all occurences at once?
Arno
Find all posts by this user
Quote this message in a reply
10-31-2017, 01:46 PM
Post: #8
RE: How do you delete characters in a string?
I don't understand your second argument to suppress there. The second argument is supposed to be an integer. I tried it verbatim and it didn't seem to change the string at all.

This brings up some issues regarding XCAS vs PRIMECAS. I notice that XCAS allows ranges like 1..3, but PRIME CAS doesn't. I'm presuming you are somehow telling CAS to use a range of string postions, but I can't tell it's format....can you explain what you are trying to do? Thanks
Find all posts by this user
Quote this message in a reply
10-31-2017, 01:52 PM
Post: #9
RE: How do you delete characters in a string?
hello

st:=st(1,4)+st(7,100);
will remove elements 5 to 6 of the string.
100 can be changed into a 1e9 if/as needed

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
10-31-2017, 11:05 PM
Post: #10
RE: How do you delete characters in a string?
(10-31-2017 11:59 AM)webmasterpdx Wrote:  So, the optimal way to drop characters in a string is as follows.

Code:

// Drops characters from string
// S   string
// L   Position before first character to drop
// R   Position after last character to drop
// e.g. S:=STRDEL(S,3,7); drops S(4), S(5) and S(6) from S.
EXPORT STRDEL(S,L,R)
BEGIN
RETURN LEFT(S,L)+RIGHT(S,DIM(S)-R+1);
END; // BEGIN...

This doesn't work if you want to drop the first or the last character of the string.
STRDEL("ABCDEFGHIJK",0,7) returns "ABCDEFGHIJKGHIJK"

Here is a working version using Cyrille substrings notation:

Code:
EXPORT STRDEL(S,L,R)
BEGIN
 LOCAL d:=DIM(S);
 IFTE(L<1,"",S(1,L))+IFTE(R>d,"",S(R,d));
END; // BEGIN...

STRDEL("ABCDEFGHIJK",0,7) returns "GHIJK"
Find all posts by this user
Quote this message in a reply
10-31-2017, 11:46 PM
Post: #11
RE: How do you delete characters in a string?
(10-31-2017 03:22 AM)Tim Wessman Wrote:  Do it the longer way or else wait a bit.

Tim, I thought you were hinting at a little gem to look forward to in the next release...
I discovered "suppress" already exists just in time to include a reference to it in my new list processing API.
http://www.hpmuseum.org/forum/thread-9411.html

Stephen Lewkowicz (G1CMZ)
List API V1.1: http://www.hpmuseum.org/forum/thread-9411.html
Visit this user's website Find all posts by this user
Quote this message in a reply
11-01-2017, 03:07 AM
Post: #12
RE: How do you delete characters in a string?
if you have a string, s, in CAS mode, type s(1,1e9)

It reboots the Prime!!!!!
Find all posts by this user
Quote this message in a reply
11-01-2017, 03:23 AM
Post: #13
RE: How do you delete characters in a string?
The substring notation method is actually a little faster than the RIGHT/LEFT method. With a 20000 loop, it's about 0.1 times faster...i.e. RIGHT/LEFT takes about 10 seconds and S(I,N) method takes about 9 seconds.
Find all posts by this user
Quote this message in a reply
11-01-2017, 04:11 AM (This post was last modified: 11-01-2017 04:11 AM by webmasterpdx.)
Post: #14
RE: How do you delete characters in a string?
So, this is seems to be the most optimized version for now.

Code:

// Drop chars from string.
// S   String.
// L   Pos of leftmost char before deletion.
// R   Pos of first char on right after deletion.
EXPORT STRDEL(S,L,R)
BEGIN
LOCAL N:=DIM(S);
RETURN IFTE(L<1,"",S(1,L))+IFTE(R>N,"",S(R,N)); // N just has to go past end of string
END; // BEGIN...
Find all posts by this user
Quote this message in a reply
Post Reply 




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