Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are sub-strings strings?
#6
@ahenry3068 

I really like the _mem approach, so I decided to tackle the ridiculous limit on strings. I’ve modified the program to support dynamic strings since the length is known, even if it varies. It seems to work fine. For now, at least...


Code: (Select All)

Dim S As String

S = "ABCDEF "
Print S
Print
SwapChars S$, 2, 3 'ACBDEF
Print S

SwapChars S$, 3, 5 'ACEDBF'

Print S
End

Sub SwapChars (s As String, idx1 As Long, idx2 As Long)
    Dim L As Long: L = Len(s)
    If idx1 < 1 Or idx2 < 1 Then Exit Sub
    If idx1 > L Or idx2 > L Then Exit Sub
    If idx1 = idx2 Then Exit Sub

    Dim m As _MEM
    m = _MemNew(L)

    ' copy string data to own buffer
    _MemPut m, m.OFFSET, s

    Dim b1 As _Unsigned _Byte, b2 As _Unsigned _Byte
    _MemGet m, m.OFFSET + (idx1 - 1), b1
    _MemGet m, m.OFFSET + (idx2 - 1), b2
    _MemPut m, m.OFFSET + (idx1 - 1), b2
    _MemPut m, m.OFFSET + (idx2 - 1), b1

    ' construct new string
    s = Space$(L)
    Dim i As Long, bt As _Unsigned _Byte
    For i = 0 To L - 1
        _MemGet m, m.OFFSET + i, bt
        Mid$(s, i + 1, 1) = Chr$(bt)
    Next

    _MemFree m
End Sub


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 678 12-20-2025, 09:28 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)