Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are sub-strings strings?
#17
Just for fun, and maybe someone else already did this (I'm late tot the party)...

Code: (Select All)
Dim Shared t$
t$ = "Pete's Tremendous. Steve's Amazing.": Print t$
swapx Mid$(t$, 8, 10), Mid$(t$, 28, 7)
Print t$
Print
t$ = "My apple is green. Your tomatoe is red.": Print t$
swapx Mid$(t$, 4, 5), Mid$(t$, 25, 7)
Print t$

Sub swapx (b$, a$)
    i = Len(t$) - (InStr(t$, a$) - 1 + Len(a$))
    j = InStr(t$, b$) - 1
    k = j + Len(b$) + 1
    temp1$ = Mid$(t$, 1, j) + a$
    temp2$ = Mid$(Mid$(t$, k), 1, Len(Mid$(t$, k)) - Len(a$) - i) + b$
    t$ = temp1$ + temp2$ + Mid$(t$, Len(temp1$) + Len(temp2$) + 1)
End Sub

Like I said, just for fun, because it isn't bullet proof. Since it uses INSTR() we can thuck fings up by working with sentences that have replacement parts repeated in the sentence. In other words, INSTR() would always pick the first occurrence, even if that's not the one your wanted swapped in the sentence.

Now if you were okay with not making the sub-strings, as you called them, hard coded, you could DIM shared the variables used to determine those sub-strings and write the algorithm differently. Like I said, this was fun, because I decided to see if I could come up with anything using those hard coded values, and I sorta did.

Pete

Oh, and swap that first sentence back before Steve and I sue your ASCII!
Reply


Messages In This Thread
Are sub-strings strings? - by PhilOfPerth - 12-26-2025, 04:14 AM
RE: Are sub-strings strings? - by ahenry3068 - 12-26-2025, 04:34 AM
RE: Are sub-strings strings? - by PhilOfPerth - 12-26-2025, 04:44 AM
RE: Are sub-strings strings? - by ahenry3068 - 12-26-2025, 04:50 AM
RE: Are sub-strings strings? - by SMcNeill - 12-26-2025, 06:01 AM
RE: Are sub-strings strings? - by Petr - 12-26-2025, 10:41 AM
RE: Are sub-strings strings? - by justsomeguy - 12-26-2025, 11:08 AM
RE: Are sub-strings strings? - by Dav - 12-26-2025, 03:56 PM
RE: Are sub-strings strings? - by SMcNeill - 12-26-2025, 05:06 PM
RE: Are sub-strings strings? - by bplus - 12-26-2025, 05:15 PM
RE: Are sub-strings strings? - by SMcNeill - 12-26-2025, 05:20 PM
RE: Are sub-strings strings? - by Petr - 12-26-2025, 05:57 PM
RE: Are sub-strings strings? - by Kernelpanic - 12-26-2025, 06:11 PM
RE: Are sub-strings strings? - by Dav - 12-26-2025, 07:05 PM
RE: Are sub-strings strings? - by bplus - 12-26-2025, 08:43 PM
RE: Are sub-strings strings? - by SMcNeill - 12-26-2025, 09:32 PM
RE: Are sub-strings strings? - by Pete - 12-26-2025, 09:50 PM
RE: Are sub-strings strings? - by Kernelpanic - 12-26-2025, 10:42 PM
RE: Are sub-strings strings? - by SMcNeill - 12-26-2025, 11:24 PM
RE: Are sub-strings strings? - by Kernelpanic - 12-27-2025, 01:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print strings with accents and special chars Ikerkaz 8 677 12-20-2025, 09:28 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: