LoginLogin

SUBST$ replacement using [ ]

Root / Submissions / [.]

12Me21Created:
string$[I] == MID$(string$,I,1) string$[I]=replace$ == string$=SUBST$(string$,I,1,replace$) So, for example:
S$="ABC"
S$[1]=""
?S$
AC

for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
Uh, no. The "best way" is not the best way, it is broken. If two spaces are in a row, only one will be removed. How about:
WHILE (INSTR(STRING$," ")>=0)
 STRING$[INSTR(STRING$," ")]=""
WEND

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
oops, I meant to add DEC I I bet INSTR is pretty slow

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]="':DEC I
NEXT

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
I bet INSTR is pretty slow How much do you bet?

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
I would say calling it twice like that is slower than it should be. And you can also avoid searching characters you've already searched.
I%=0
REPEAT
I%=INSTR(I%,STRING$," ")
IF I%<0 THEN BREAK
STRING$[I%]=""
UNTIL FALSE

Replying to:12Me21
for example, if you want to remove spaces you might do
NEWSTRING$=""
FOR I=0 TO LEN(STRING$)-1
 IF MID$(STRING$,I,1)!=" " THEN NEWSTRING$=NEWSTRING$+MID$(STRING$,I,1)
NEXT
or better yet:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$=SUBST$(STRING$,I,"",1):DEC I
NEXT
but the best way is:
FOR I=0 TO LEN(STRING$)-1
 IF STRING$[I]==" " THEN STRING$[I]=""
NEXT
(it seems so obvious, why is this not more well known)
S$=" HELL O! !!"

GOTO@M
WHILE I+1S$[I]=""@M
I=INSTR(I,S$," ")WEND

?S$
I found a use for GOTO ...

I've gotten so used to this feature that it's really annoying to deal with other langauges like JavaScript that don't have mutable strings.