String Search, Replace or Delete Routine (Method 1)

Input: Str0 = the full/original string

Input: Str1 = the string to search for (can be any length)

Input: Str2 = the string that replaces all occurrences of Str1 in Str0. (can be any length) (If you want to delete all occurrences of instead of replacing them, then make the length of Str2 to be zero, and it will do just that.)

Output: Str3 = the resulting string.

Variables used: L,M,Z, Str0, Str1, Str2, Str3.

Note that I can tell that this code is quite slow, so I was hoping that someone could optimize it or translate it to asm?

I programmed this search routine in 3 other ways, one of them I believe to also be bug free, but it's a lot longer and it's slower for a longer Str0 which has many spaced out occurrences of Str1. (Shown below).

String Search, Replace or Delete Routine (Method 2)

It has the same input and output as the program above, and it uses all of the same variables plus the following:

lists: P, S, T, X

Reals: C,N

Basically instead of searching and replacing simultaneously, this approach searches first and stores data in lists P, S, and T (list X is just a temporary list).

List P = the indexed locations of the first character of each occurrence of string Str1 in Str0.

List T = the start index(es) where Str1 is not in Str0.

List S = the number of spaces between each occurrence of Str1 in Str0.

So that we can literally construct Str0 using data from lists P,T, and S, as far as the search Str1 is concerned when we want to replace occurrences of the search.

Starting from Lbl 1, is the replace routine which replaces all Str1 occurrences in Str0 with Str2 or it deletes them if the length of Str2 (N) is zero.

I doubt experienced coders will need comments for the first program, but if anyone is interested in full comments for the second program, I can submit them.

I have been a member for a little while, but this is my first post! I have made several routines that have been useful for me over the years, but I have seen some impressive routines in this thread.

Input: Str0 = the full/original string

Input: Str1 = the string to search for (can be any length)

Input: Str2 = the string that replaces all occurrences of Str1 in Str0. (can be any length) (If you want to delete all occurrences of instead of replacing them, then make the length of Str2 to be zero, and it will do just that.)

Output: Str3 = the resulting string.

Variables used: L,M,Z, Str0, Str1, Str2, Str3.

**Code:**```
:length(Str0→L
```

:length(Str1→M

:" →Str3

:1→Z

:While Z+M-1≤L

: If Str1=sub(Str0,Z,M

: Then

: If 0≠length(Str2

: Str3+Str2→Str3

: Z+M→Z

: Else

: Str3+sub(Str0,Z,1→Str3

: Z+1→Z

: End

:

: If Z+M-1>L and M>1

: 1→M

:End

:

:If 1<length(Str3

:sub(Str3,2,length(Str3)-1→Str3

Note that I can tell that this code is quite slow, so I was hoping that someone could optimize it or translate it to asm?

I programmed this search routine in 3 other ways, one of them I believe to also be bug free, but it's a lot longer and it's slower for a longer Str0 which has many spaced out occurrences of Str1. (Shown below).

String Search, Replace or Delete Routine (Method 2)

It has the same input and output as the program above, and it uses all of the same variables plus the following:

lists: P, S, T, X

Reals: C,N

Basically instead of searching and replacing simultaneously, this approach searches first and stores data in lists P, S, and T (list X is just a temporary list).

List P = the indexed locations of the first character of each occurrence of string Str1 in Str0.

List T = the start index(es) where Str1 is not in Str0.

List S = the number of spaces between each occurrence of Str1 in Str0.

So that we can literally construct Str0 using data from lists P,T, and S, as far as the search Str1 is concerned when we want to replace occurrences of the search.

Starting from Lbl 1, is the replace routine which replaces all Str1 occurrences in Str0 with Str2 or it deletes them if the length of Str2 (N) is zero.

**Code:**```
:0→dim(∟P
```

:0→dim(∟T

:0→dim(∟S

:length(Str0→L

:length(Str1→M

:length(Str2→N

:

:1→C:1→Z

:While Z+M-1≤L

: If Str1=sub(Str0,Z,M

: Then

: Z→∟P(C

: C+1→C:Z+M→Z

: Else

: Z+1→Z

: End

:End

:

:If 0=dim(∟P

:Then

: Str0→Str3

: Return

:End

:

:If 1=dim(∟P

:Then

: If 1=∟P(1

: Then

: If L≠M

: Then

: M+1→∟T(1

: L-M→∟S(1

: End

: Else

: {∟P(1)-1→S

: {1→T

: If L≠M-1+∟P(1

: Then

: L-M-∟S(1→∟S(2

: M+∟P(1→∟T(2

: End

: End

: Goto 1

:End

:

:DeltaList(∟P→X

:

:For(Z,1,dim(∟X

: If M≠∟X(Z

: Then

: If Z=1 and 1≠∟P(1

: {M-1+∟P(1→S

: 1+dim(∟S→dim(∟S

: ∟X(Z→∟S(dim(∟S

: End

:End

:

:If L≠M-1+∟P(dim(∟P

:Then

: 1+dim(∟S→dim(∟S

: L+1-∟P(dim(∟P→∟S(dim(∟S

:End

:

:If 0≠dim(∟S

:Then

: ∟S-M→S

:Else

: Goto 1

:End

:

:∟P+M→X

:1+dim(∟P→dim(∟P

:For(Z,1,dim(∟X

: If ∟X(Z)≠∟P(Z+1

: Then

: If Z=1 and 1≠∟P(1

: {1→T

: 1+dim(∟T→dim(∟T

: ∟X(Z→∟T(dim(∟T

: End

:End

:

:If L<∟T(dim(∟T

: dim(∟T)-1→dim(∟T

:dim(∟P)-1→dim(∟P

:

:Lbl 1

:" →Str3

:If 0=dim(∟T

:Then

: 1→Z

: While Z≤dim(∟P

: If N≠0

: Str3+Str2→Str3

: Z+1→Z

: End

:

: If N=0

: "→Str3

: Goto 3

:End

:

:1→C:1→Z

:Lbl 2

:While Z≤dim(∟P) and C≤dim(∟T

: If ∟P(Z)<∟T(C

: Then

: If N≠0

: Str3+Str2→Str3

: 1+Z→Z

: Else

: Str3+sub(Str0,∟T(C),∟S(C→Str3

: 1+C→C

: End

: Goto 2

:End

:

:If L=M-1+∟P(dim(∟P

:Then

: 1+L-∟T(dim(∟T))-∟S(dim(∟S→C

: C/M→C

: While C≠0

: If N≠0

: Str3+Str2→Str3

: C-1→C

: End

:Else

: Str3+sub(Str0,∟T(dim(∟T)),∟S(dim(∟S→Str3

:End

:

:Lbl 3

:If 0≠length(Str3

: sub(Str3,2,length(Str3)-1→Str3

I doubt experienced coders will need comments for the first program, but if anyone is interested in full comments for the second program, I can submit them.

I have been a member for a little while, but this is my first post! I have made several routines that have been useful for me over the years, but I have seen some impressive routines in this thread.