How do you delete characters in a string? - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: How do you delete characters in a string? (/thread-9406.html) |
How do you delete characters in a string? - webmasterpdx - 10-31-2017 03:13 AM 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? RE: How do you delete characters in a string? - Tim Wessman - 10-31-2017 03:22 AM Do it the longer way or else wait a bit. RE: How do you delete characters in a string? - webmasterpdx - 10-31-2017 03:29 AM What do you mean wait a bit? RE: How do you delete characters in a string? - Tim Wessman - 10-31-2017 03:56 AM "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... RE: How do you delete characters in a string? - webmasterpdx - 10-31-2017 05:07 AM Ah, suppress works! ....as in S:=suppress(S,3); RE: How do you delete characters in a string? - webmasterpdx - 10-31-2017 11:59 AM 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:
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. RE: How do you delete characters in a string? - Arno K - 10-31-2017 12:26 PM Did you try something like "SUPPRESS(s,"2357")" which deletes all occurences at once? Arno RE: How do you delete characters in a string? - webmasterpdx - 10-31-2017 01:46 PM 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 RE: How do you delete characters in a string? - cyrille de brébisson - 10-31-2017 01:52 PM 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 RE: How do you delete characters in a string? - Didier Lachieze - 10-31-2017 11:05 PM (10-31-2017 11:59 AM)webmasterpdx Wrote: So, the optimal way to drop characters in a string is as follows. 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) STRDEL("ABCDEFGHIJK",0,7) returns "GHIJK" RE: How do you delete characters in a string? - StephenG1CMZ - 10-31-2017 11:46 PM (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 RE: How do you delete characters in a string? - webmasterpdx - 11-01-2017 03:07 AM if you have a string, s, in CAS mode, type s(1,1e9) It reboots the Prime!!!!! RE: How do you delete characters in a string? - webmasterpdx - 11-01-2017 03:23 AM 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. RE: How do you delete characters in a string? - webmasterpdx - 11-01-2017 04:11 AM So, this is seems to be the most optimized version for now. Code:
|