UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> ReplaceEWQ    
CODE
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' Original submission by Walter Niesz
' May 30, 2013
'
' You are free to use this code in any application,
' provided the copyright notice is left unchanged.

Public Function ReplaceEWQ(Data As String, String1 As String, String2 As String, Optional RemoveQuotes As Boolean = False) As String

' DESCRIPTION    
' This function replaces a delimiter of a string of data
' with another one, except when it is inside of double quotes.
' (think of it as "Replace, Except Within Quotes")
'
' This is useful when parsing a CSV file that has data like:
'
' 123,"abc,def",456
'
' Using the following:
'
'      YourString = ReplaceEWQ(YourString, ",", "|")
'
' ... will render the data like:
'
' 123|"abc,def"|456
'
' ... so that you can split the data into an array using SPLIT()
' without splitting apart a quote delimited field.
'
' You can optionally remove the quotes during the replace process


   Dim i as Long
   Dim myArray() As String
   
   myArray = Split(Data, """")
   
   For i = 0 To UBound(myArray)
       If i Mod 2 = 0 Then
           ReplaceEWQ = ReplaceEWQ & Replace(myArray(i), String1, String2)
       Else
           ReplaceEWQ = ReplaceEWQ & """" & myArray(i) & """"
       End If
   Next i
   
   If RemoveQuotes Then
       ReplaceEWQ = Replace(ReplaceEWQ, """", "")
   End If
   
End Function


Creative Commons License
ReplaceEWQ by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 2,363 times.  This page was last modified 17:27, 30 May 2013 by Walter Niesz.   Disclaimers