LoginLogin
Might make SBS readonly: thread

ISINSTR (String Searching)

Root / Submissions / [.]

MinxrodCreated:
I made a semisimple program that produces a list of substring locations within a string.
DEF ISINSTR(string$,sub$)
 DIM INLIST%[0]
 VAR I%,J%
 FOR I%=0 TO LEN(string$)-1
  J% = INSTR(I%,string$,sub$)
  IF I% == J% THEN PUSH INLIST%,I%
 NEXT
 RETURN INLIST%
END
To use, just copy the DEF block or make it a COMMON DEF etc. that way~ and say
arrayvarname=ISINSTR("string here","e")
How? This basically iterates through and checks for all possible occurrences of a substring within a string. When it finds one, it checks if it was found where it was looking or if INSTR was searching ahead. If it is where it should be, it adds that location to the list (which will be 0 in length if no match is found.) Or this one by SquareFingers that he says is better (looks like it XD):
DEF ISINSTR(string$,sub$)
 DIM INLIST%[0]
 VAR I%
 I%=INSTR(string$,sub$)
 WHILE (I%>=0)
  PUSH INLIST%,I%
  I%=INSTR(I%+1,string$,sub$)
 WEND
 RETURN INLIST%
END
Thank you for improving this!

Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END

Replying to:SquareFingers
Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END
WHILE I!=-1 would probably be better, but still good.

Replying to:SquareFingers
Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END
I have come across instances where some developer says something like "Hey, it'd be super neat and useful if we could distinguish between two types of 'failure': instead of -1 meaning 'failure', we'll have -1 mean 'failure A' and -2 mean 'failure B'!". It's a bad idea (if you value backwards compatibility), but it happens. That's why I chose that guard. I don't expect INSTR, in particular, will suffer this fate, but you never know when/how this kind of thing happens, and so I make it a practice to use guards that are as general as possible/reasonable. Future-proofing against stupid 'improvements'.

Replying to:SquareFingers
Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END
This is nice! Is it OK if I added it to the page? It's better than mine XD

Replying to:SquareFingers
Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END
Sorry I didn't reply sooner. Certainly, you have permission. One more improvement would be to use the % suffix on both I and INLIST.

Replying to:SquareFingers
Simpler, and quicker:
DEF ISINSTR(string$,sub$)
 DIM INLIST[0]
 VAR I
 I=INSTR(string$,sub$)
 WHILE (I>=0)
  PUSH INLIST,I
  I=INSTR(I+1,string$,sub$)
 WEND
 RETURN INLIST
END
Oh yeah: Integers are faster, aren't they. I started regularly suffixing variables about a week after I finished the program, so my code is still kind of messy :) I'll add that to both of the DEFs, thank you for the input :D

Very useful!

I don't understand what this is for

Replying to:HumbleWario
I don't understand what this is for
It makes a list of the occurrences of a string in another string. for example, ISINSTR("TEST TEST TESTING","TEST") would return an array containing 0,5,10