Results 1 to 40 of 41

Thread: QuoteSplit challenge

Threaded View

  1. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: QuoteSplit challenge

    I'm tired so this code probably has bugs but I tested it a little. You'll probably find some if you test it.

    I'll get back to it later after I get some sleep.

    I provided a Start argument so you can choose where to start splitting from and also (hopefully) have the limit part working.

    It's probably buggy as hell but here it is:

    VB Code:
    1. Option Explicit
    2.  
    3. 'Count the number of instances a string appears within another string.
    4. Private Function InStrCount(ByVal Start As Long, ByRef Expression As String, ByRef SearchFor As String, _
    5.     Optional ByRef CompareMethod As VbCompareMethod = vbBinaryCompare) As Long
    6.    
    7.     If Start = 0 Then Exit Function
    8.     If Start > Len(Expression) Then Exit Function
    9.    
    10.     Dim lonPos As Long, lonCount As Long
    11.    
    12.     lonPos = InStr(Start, Expression, SearchFor, CompareMethod)
    13.    
    14.     Do While lonPos > 0
    15.         lonCount = lonCount + 1
    16.        
    17.         lonPos = InStr(lonPos + 1, Expression, SearchFor, CompareMethod)
    18.     Loop
    19.    
    20.     InStrCount = lonCount
    21.    
    22. End Function
    23.  
    24. Public Function QuoteSplit(ByVal Start As Long, ByRef Expression As String, Optional ByRef Delimiter As String = " ", _
    25.     Optional ByVal Limit As Long = -1, Optional ByVal CompareMethod As VbCompareMethod = vbBinaryCompare) As String()
    26.    
    27.     Dim lonLenEXP As Long
    28.    
    29.     lonLenEXP = Len(Expression)
    30.    
    31.     'Quick input checks...
    32.     If lonLenEXP = 0 Then Exit Function     'Expression is empty.
    33.     If Start = 0 Then Exit Function         'Start position is 0.
    34.     If Start > lonLenEXP Then Exit Function 'Start position is greater than length of expression.
    35.    
    36.     Dim strRet() As String 'Return value.
    37.     Dim lonPos As Long 'Position of delimiter.
    38.     Dim lonPrevPos As Long 'Previous position of delimiter.
    39.     Dim lon3Pos As Long 'Previous position of previous delimiter.
    40.     Dim bytInQuote As Byte 'Currently in quotations?
    41.     Dim lonQuoteCount As Long 'Number of quotes found.
    42.     Dim lonCount As Long 'Number of delimiters found (used for Limit argument).
    43.     Dim strTemp As String 'Temp string.
    44.     Dim lonStart As Long 'Temp start position.
    45.     Dim lonEnd As Long 'Temp long position.
    46.     Dim lonLenDelim As Long 'Length of delimiter.
    47.     Dim lonBnd As Long 'UBound of return value.
    48.     Dim strTmpCheck As String
    49.    
    50.     lonLenDelim = Len(Delimiter)
    51.    
    52.     'Find first delimiter.
    53.     lonPos = InStr(Start, Expression, Delimiter, CompareMethod)
    54.    
    55.     'Quick check.
    56.     If lonPos = 0 Then GoTo NoDelimiter
    57.    
    58.     'Keep looping while we can still find the delimiter.
    59.     Do While lonPos > 0
    60.        
    61.         'this,is,"a,test",abc
    62.        
    63.         '  "this,is",a,test
    64.         If lonPrevPos = 0 Then 'This is the first delimiter.
    65.             'Check if we're within quotes.
    66.             strTemp = Mid$(Expression, 1, lonPos - 1)
    67.            
    68.             lonQuoteCount = InStrCount(1, strTemp, Chr$(34), vbBinaryCompare)
    69.            
    70.             If lonQuoteCount Mod 2 = 0 Then
    71.                 bytInQuote = 0
    72.             Else
    73.                 bytInQuote = 1
    74.             End If
    75.            
    76.             If bytInQuote = 0 Then
    77.                 'Not in quotes.
    78.                 ReDim strRet(0) As String
    79.                
    80.                 strRet(0) = strTemp
    81.                 lonPrevPos = lonPos
    82.                
    83.                 lonCount = lonCount + 1
    84.                
    85.                 'Check if we're done.
    86.                 If lonCount = Limit Then
    87.                     QuoteSplit = strRet()
    88.                    
    89.                     Erase strRet()
    90.                     Exit Function
    91.                 End If
    92.                
    93.             Else
    94.                 lonPos = lonPos + 1
    95.             End If
    96.            
    97.         Else 'This is NOT the first delimiter.
    98.        
    99.             'this,is,"a,test",abc
    100.        
    101.             '  "this,is",a,test
    102.            
    103.             'Get text from previous delimiter to this one.
    104.             lonStart = lonPrevPos + lonLenDelim
    105.             lonEnd = lonPos
    106.            
    107.            
    108.             strTemp = Mid$(Expression, lonStart, lonEnd - lonStart)
    109.            
    110.             'Check if we're inside quotes.
    111.             lonQuoteCount = InStrCount(1, strTemp, Chr$(34), vbBinaryCompare) + CLng(bytInQuote)
    112.            
    113.             If lonQuoteCount Mod 2 = 0 Then
    114.                 bytInQuote = 0
    115.             Else
    116.                 bytInQuote = 1
    117.             End If
    118.            
    119.             If bytInQuote = 0 Then
    120.                 'Not in quotes.
    121.                 lonBnd = UBound(strRet()) + 1
    122.                
    123.                 ReDim Preserve strRet(0 To lonBnd) As String
    124.                
    125.                 strRet(lonBnd) = strTemp
    126.                
    127.                 lonCount = lonCount + 1
    128.                 lonPrevPos = lonPos
    129.                
    130.                 'Check if we're done.
    131.                 If lonCount = Limit Then
    132.                     QuoteSplit = strRet()
    133.                    
    134.                     Erase strRet()
    135.                     Exit Function
    136.                 End If
    137.                
    138.             Else
    139.                 lonPrevPos = lonPrevPos + 1
    140.             End If
    141.        
    142.         End If
    143.        
    144.         lonPos = InStr(lonPos + lonLenDelim, Expression, Delimiter, CompareMethod)
    145.     Loop
    146.    
    147.     'Check for text at end of string.
    148.     If lonPrevPos < lonLenEXP Then
    149.         lonBnd = UBound(strRet()) + 1
    150.        
    151.         ReDim Preserve strRet(0 To lonBnd) As String
    152.        
    153.         strRet(lonBnd) = Mid$(Expression, lonPrevPos + lonLenDelim)
    154.     End If
    155.    
    156.     QuoteSplit = strRet()
    157.    
    158.     Erase strRet()
    159.    
    160.     Exit Function
    161.    
    162. NoDelimiter:
    163.     'Just return the text like the Split() function does.
    164.     ReDim strRet(0) As String
    165.    
    166.     strRet(0) = Expression
    167.     QuoteSplit = strRet()
    168.    
    169.     Erase strRet()
    170. End Function
    171.  
    172. 'Test the function.
    173. Private Sub Command1_Click()
    174.     Dim strBuffer() As String
    175.    
    176.     strBuffer() = QuoteSplit(1, Text1.Text, "**")
    177.    
    178.     Dim intLoop As Integer
    179.    
    180.     On Error Resume Next 'For UBound error.
    181.    
    182.     List1.Clear
    183.    
    184.     For intLoop = 0 To UBound(strBuffer())
    185.         List1.AddItem strBuffer(intLoop)
    186.     Next intLoop
    187.    
    188. End Sub
    Last edited by DigiRev; Mar 26th, 2007 at 02:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width