Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

SplitQuoted - A split variant that deals correctly with quoted elements
[ All Languages » VB »  String]

Total Hit ( 1782)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' split a string, dealing correctly with quoted items
'
' TEXT is the string to be split
' SEPARATOR is the separator char (default is comma)
' QUOTES is the character used to quote strings (default is """",
' the double quote)
'  you can also use a character pair (eg "{}") if the opening
'  and closing quotes are different
'
' for example you can split the following string
'   arr() = SplitQuoted("[one,two],three,[four,five]", , "[]")
' into 3 items, because commas inside []
' are not taken into account

' UPDATE: Thanks to Kevan Brown, Chief Technology Officer, CommercExec, Inc.
' for spotting an error that made the function fail under certain conditions

Function SplitQuoted(ByVal Text As String, Optional ByVal Separator As String = _
  ",", Optional ByVal Quotes As String = """") As String()
  ReDim res(100) As String
  Dim resCount As Long
  Dim index As Long
  Dim startIndex As Long
  Dim endIndex As Long
  Dim length As Long
  Dim sepCode As Integer
  Dim openCode As Integer
  
  length = Len(Text)
  
  ' a null string is a special case
  ' return the same uninitialized error that Split would return
  If length = 0 Then
    SplitQuoted = Split("")
    Exit Function
  End If
  
  ' integer ASCII codes of separators
  sepCode = Asc(Separator)
  openCode = Asc(Quotes)
  
  startIndex = 1
  
  Do While index < length
    index = index + 1
    
    Select Case Asc(Mid$(Text, index, 1))
      Case sepCode
        ' we've found the end of an item
        ' if endIndex<>0 then the item is quoted
        If endIndex = 0 Then endIndex = index
        ' make room in the array, if necessary
        If resCount > UBound(res) Then
          ReDim Preserve res(0 To resCount + 99) As String
        End If
        'store the element
        res(resCount) = Mid$(Text, startIndex, endIndex - startIndex)
        resCount = resCount + 1
        ' prepare for next element
        startIndex = index + 1
        endIndex = 0
        
      Case openCode
        startIndex = index + 1
        ' search for the closing quote
        endIndex = InStr(startIndex, Text, Right$(Quotes, 1))
        index = endIndex
    End Select
    
  Loop
  
  ' store the last item
  If endIndex = 0 Then endIndex = length + 1
  ' trim or expand the array, as necessary
  ReDim Preserve res(0 To resCount) As String
  ' store the element
  res(resCount) = Mid$(Text, startIndex, endIndex - startIndex)
  
  SplitQuoted = res()
  
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.