Get String After Last Delimiter (BASIC)

128 Views Asked by At

I am using BASIC programming language for work. I have an example string here:

Eric*01*73839

By using the field function, I can grab either Eric or 01 like so:

ERIC = FIELD(STRING,"*",1)
01 = FIELD(STRING,"*",2)

However, if I wanted to grab the '73839' portion, how would this be done? I found it hard to find resources on google since it is so OLD.

**No, FIELD(STRING,"*",3) doesn't work.

2

There are 2 best solutions below

0
On

You could always do some variation of converting the plain string to a dynamic string and extracting the third field. For example:

CONVERT "*" TO @FM IN STRING
FIELD3 = STRING<3>

The specifics will depend on which flavor of multi-value BASIC you're using. It might have to be more like:

EQU FM TO CHAR(253)    ;* Likely somewhere near the top, it will be used a lot
...
STRING = CHANGE(STRING,"*",FM)
FIELD3 = STRING<3>

If it needs to be more dynamic (there may be more than three fields and you always need the last one), you'll need to use DCOUNT (or COUNT+1) to find the last field.

0
On

Something isn't right with either your code or your data (could that second * possibly be a unicode lookalike [https://www.htmlsymbols.xyz/star-symbols/asterisk]?). Or is STRING perhaps a reserved word in your flavor of PICK Basic?

STRING='Eric*01*73839'
CRT '1=':FIELD(STRING,'*',1)
CRT '2=':FIELD(STRING,'*',2)
CRT '3=':FIELD(STRING,'*',3)

Results in this for me:

1=Eric
2=01
3=73839