Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Search and Replace String
Author:
James Tracy
E-mail:
Click to e-mail author
Website:
http://www.ProjectOmicron.net
Submitted:
1/30/2004
Version:
VB3, VB4, V
Compatibility:
VB3, VB4, VB5, VB6, VB.NET
Category:
String Manipulation
Views:
39326
Bulletproof search and replace of string.
Declarations:
'None
Code:
Function SandR (ByVal SearchString As String, ByVal LookFor As String, ByVal ReplaceWith As String) As String Rem +-----------------------------------------------------------------------+ Rem | SearchString = String to search. | Rem | LookFor = String to look for within SearchString | Rem | ReplaceWith = What to replace SearchString | Rem | | Rem | Test=SandR("Mary Had a little lamb","a","aa") | Rem | would return: | Rem | Maary Haad aa little laamb | Rem | | Rem | Compatibility: Visual Basic 2.0 | Rem | | Rem | Programmed by: JamesTracy95820@hotmail.com | Rem | | Rem +-----------------------------------------------------------------------+ Rem +-----------------------------------+ Rem | Declair variables | Rem +-----------------------------------+ Dim LeftPart As String Dim RightPart As String Dim Location As Integer Dim LeftLocation As Integer Dim RightLocation As Integer Rem +-------------------------------+ Rem | Initilize variables | Rem +-------------------------------+ LeftPart = "" RightPart = "" Location = 0 Rem ========================================================== If Len(LookFor) = 0 Then LeftPart = SearchString Else If LookFor = ReplaceWith Then LeftPart = SearchString Else If Len(SearchString) = 0 Then LeftPart = SearchString Else LeftPart = "" RightPart = SearchString Rem ============================================ Do Location = InStr(1, RightPart, LookFor, 0) ' Case INsensitive If Location = 0 Then LeftPart = LeftPart + RightPart Else If Location = 1 Then LeftPart = LeftPart + ReplaceWith RightLocation = Location + Len(LookFor) If RightLocation > Len(RightPart) Then RightPart = "" Else RightPart = Mid(RightPart, RightLocation) End If Else If Location >= 2 Then LeftLocation = Location - 1 RightLocation = Location + Len(LookFor) LeftPart = LeftPart + Left(RightPart, LeftLocation) + ReplaceWith If RightLocation > Len(RightPart) Then RightPart = "" Else RightPart = Mid(RightPart, RightLocation) End If End If End If End If Loop Until Location = 0 End If End If End If SandR = LeftPart ' Return string End Function
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement