I thought that this may be useful to some of you developers out there. This is a replacement for the "Split" Function in vb. It splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes. Im using this for some custom commands but im sure some of you might find it useful for some other purpose. (It would be good for a simple protocol).
Public Function Seperate(strCode As String) As Variant
'''By Atom'''
Dim arrTemp() As Variant, strTemp As String, i As Integer, x As Integer
Dim LastChar As String, Char As String, inQuotes As Boolean
For i = 1 To Len(strCode)
LastChar = Char
Char = Mid(strCode, i, 1)
If inQuotes = False Then
If Char = " " Then
If LastChar <> " " Then
strTemp = strTemp & Char
End If
Else
strTemp = strTemp & Char
If Char = Chr(34) Then inQuotes = True
End If
Else
strTemp = strTemp & Char
If Char = Chr(34) Then inQuotes = False
End If
Next i
If Left(strTemp, 1) = " " Then strTemp = Mid(strTemp, 2)
If Right(strTemp, 1) = " " Then strTemp = Left(strTemp, Len(strTemp) - 1)
inQuotes = False
ReDim Preserve arrTemp(0)
x = 0
For i = 1 To Len(strTemp)
Char = Mid(strTemp, i, 1)
If Char = " " Then
If inQuotes = False Then
x = x + 1
ReDim Preserve arrTemp(x)
Else
arrTemp(x) = arrTemp(x) & Char
End If
ElseIf Char = Chr(34) Then
If inQuotes = False Then
inQuotes = True
Else
x = x + 1
ReDim Preserve arrTemp(x)
End If
Else
arrTemp(x) = arrTemp(x) & Char
End If
Next i
Seperate = arrTemp
End Function
Explain "as variant". Why'd you use it? What does it do?
so that it returns as an array
Thanks, *atom*
;D
Actually that's rather usless to me
atleast he replyed huh ::)
when you reply, why don't you post something we actually care about, warz? :-/
You misspelled Separate :(
Misspelled words in code are fatal, especially when working in programming teams. In a hypothetical situation a different programmer might try to call the nonexistent "Separate" function, get a compiler error, look at the code, see a "Seperate" function, scratch his/head several times, <repeat>.
What is wrong with Split()?
QuoteYou misspelled Separate :(
Misspelled words in code are fatal, especially when working in programming teams. In a hypothetical situation a different programmer might try to call the nonexistent "Separate" function, get a compiler error, look at the code, see a "Seperate" function, scratch his/head several times, <repeat>.
Not to mention words like centre/center color/colour.
Which I go trying to spell the >canadian< way :(
don't you mean center/centre color/colour ?
picky? more or less asking a stupid question...
What is wrong with Split()?
So what's wrong with Split()?
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.
I don't think split can do that?
You're right.
Quote from: iago on April 30, 2004, 06:55 AM
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.
I don't think split can do that?
Using a combination of Split() and InStr() could.
Quote from: LoRd[nK] on April 30, 2004, 10:02 AM
Quote from: iago on April 30, 2004, 06:55 AM
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.
I don't think split can do that?
Using a combination of Split() and InStr() could.
Would split be very useful at all? Unless you mean to find a substring that doesn't contain any "" and then split that separately..
RegEx could work here.