Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Find and Replace
#3
@bplus See what you think of this style string replacement:

Code: (Select All)
O$ = "12/24/2025"
Print String.Replace.Part(O$, "...??.....", "??", "31") 'string replace part allows us to set a pattern and change our original
Print String.Replace.Part(O$, "..?..?....", "?", "-") 'in relation to that pattern, which is useful for stuff like:
Print
Print
Print Date$ 'the originial version
Print String.Convert.Date(Date$, "Mm-Dd-Year", "Year-Mm-Dd") 'change the pattern from MM-DD-YYYY to YYYY-MM-DD
Print String.Convert.Date(Date$, "Mm-Dd-Year", "Dd-Mm-Year") 'to the weird other side of the world pattern of DD-MM-YYYY

Function String.Convert.Date$ (Source As String, oFormat As String, nFormat As String)
Dim T As String
T = Source
For i = 1 To Len(T)
T = String.Replace.Part(T, nFormat, Mid$(oFormat, i, 1), Mid$(Source, i, 1))
Next
String.Convert.Date = T
End Function

Function String.Get.Part$ (Source As String, Format As String, Part As String)
'this routine allows us to get partial information from a properly formatted string
'format should be something similar to:
'YYYY/MM/DD
'YY-MM-DD
'or similar. What we're looking for is the PART we specify in that format.
Dim As Long p

p = InStr(UCase$(Format), Part)
If Len(Source) <> Len(Format) _OrElse p = 0 Then
_MessageBox "Bad Format", "Error: Passing String with invalid format to Function String.Get.Part.", "error"
Else
String.Get.Part = Mid$(Source, p, Len(Part))
End If
End Function


Function String.Replace.Part$ (Source As String, Format As String, Part As String, Replacement As String)
'this routine allows us to get partial information from a properly formatted string
'format should be something similar to:
'YYYY/MM/DD
'YY-MM-DD
'or similar. What we're looking for is the PART we specify in that format.
Dim As Long p
Dim As String T
T = Source
p = InStr(Format, Part)
If Len(Source) <> Len(Format) _OrElse p = 0 _OrElse Len(Part) <> Len(Replacement) Then
_MessageBox "Bad Format", "Error: Passing String with invalid format to Function String.Replace.Part.", "error"
Else
Do
Mid$(T, p, Len(Replacement)) = Replacement
String.Replace.Part = T
p = InStr(p + 1, Format, Part)
Loop Until p = 0
End If
End Function

Instead of just saying "Replace CAT with DOG", this lets you specify patterns to replace so you can completely reformat strings and such very quick and easily.

Note that since this requires set patterns, you can't replace "APPLE" with "BANANAORANGE" as the patterns don't match. It shouldn't be too hard to swap it to do such, but that's not something I was looking for here. I'm just looking to set a pattern, a replacement pattern, and then replace those chose parts quickly and easily. Wink
Reply


Messages In This Thread
String Find and Replace - by SMcNeill - 04-22-2025, 04:41 PM
RE: String Find and Replace - by SMcNeill - 04-24-2025, 07:19 AM
RE: String Find and Replace - by SMcNeill - 9 hours ago



Users browsing this thread: 1 Guest(s)