Re: SmartReplace challenge
well, no one else has posted so I figured I'd throw something (short) together.
It supports *, Delimiters, Start and Count. It won't however allow you to include the delimiter in the Find string (i disagree with how that should be interpreted anyway) - i.e. it'll work with 1, 3 & 4 above
VB Code:
Private Function SmartReplace(sExpression As String, sFind As String, sReplace As String, _
Optional sDelimiters As String = " ", Optional ByVal Start As Long = 1, _
Optional ByVal Count As Long = -1) As String
Dim sParts() As String, sPart As String, sMatch As String
Dim N As Long, lStart As Long, lEnd As Long
sParts = Split(Replace(sFind, "*", vbNullChar), vbNullChar)
For N = 0 To UBound(sParts)
If Len(sParts(N)) > Len(sPart) Then sPart = sParts(N)
Next N
SmartReplace = sExpression
N = Start
Do
N = InStr(N, SmartReplace, sPart)
If N Then
lStart = InStrRev(SmartReplace, sDelimiters, N) + 1
lEnd = InStr(N + Len(sPart), SmartReplace, sDelimiters)
If lEnd = 0 Then lEnd = Len(SmartReplace)
If Mid$(SmartReplace, lStart, lEnd - lStart) Like sFind Then
SmartReplace = Left$(SmartReplace, lStart - 1) & sReplace & Mid$(SmartReplace, lEnd)
N = N + Len(sReplace)
Count = Count - 1
Else
N = N + 1
End If
End If
Loop While N > 0 And Count > 0
End Function
regarding comments made in the QuoteSplit thread about RegExp - there's still enough need for something like this is if you only want a simple SmartReplace, or if you perhaps don't want another dependency.
Anyway, it looks as though your attempts to revive the contests are dying a death here, Merri!
Re: SmartReplace challenge
I'm coming im coming - exams over :D.
Coursework started :(.
Well Ill try to get something done soon.
Honestly though, I'm moving to other programming languages... I've hacked VB too much. However, my Speed4VB (New name :P), will be completed sometime - replaces VB native functions with FAST x86 assembly equivalents.
Cheers
Re: SmartReplace challenge
I was looking for something just like what you posted. Thanks. It works great.
So far the only change I made was to change the count parameter to be a boolean flag for replace 1st or replace all.
Re: SmartReplace challenge
This is exactly why microsoft built the regular expression classes. Take the energy you're using to create your own parsing language, and learn one that's already been developed.
Re: SmartReplace challenge
You're writing in a wrong thread in a sense that I didn't originally think this as a great idea, either; however, people seemed to want to have a shot at this, so I put up the challenge. As you can see, it didn't quite catch up, so giving it some critic is kind of pointless. And the thread is over a year old, too.
As for regular expressions, despite them being useful sometimes, personally I find them complex to manage in a code: you really really need to study them long and hard to get good enough to be able to read them correctly right on. Thus I think regular expressions have a big negative effect on code readability. Thus my opinion: they're useful, but each time they're used the line should be heavily commented.
Re: SmartReplace challenge
Just giving a heads up to the new guy that posted two days ago.
Re: SmartReplace challenge
Hi Readwulf,
Did you manage to get your new fast x86 functions written? I've seen you've posted on the ThunderVB project with an Instr replacement some time ago. But I've found a bug in it and have also found this post. Just wondering if you've got a simpler replacement I can use within VB6.
Jon
Quote:
Originally Posted by Raedwulf
I'm coming im coming - exams over :D.
Coursework started :(.
Well Ill try to get something done soon.
Honestly though, I'm moving to other programming languages... I've hacked VB too much. However, my Speed4VB (New name :P), will be completed sometime - replaces VB native functions with FAST x86 assembly equivalents.
Cheers