• Welcome to Valhalla Legends Archive.
 

My Seperate Function

Started by Atom, December 28, 2002, 07:52 PM

Previous topic - Next topic

Atom

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
I am back! aINC is dead, ThinkTank PRO is alive.
VB, JAVA, ASM, C, its all yummy to me.

warz

#1
Explain "as variant". Why'd you use it? What does it do?

Noodlez

#2
so that it returns as an array

warz

#3
Thanks, *atom*

Atom

#4
;D
I am back! aINC is dead, ThinkTank PRO is alive.
VB, JAVA, ASM, C, its all yummy to me.

warz

#5
Actually that's rather usless to me

l)ragon

#6
atleast he replyed huh  ::)
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

haZe

#7
when you reply, why don't you post something we actually care about, warz? :-/

Yoni

#8
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>.

Grok

#9
What is wrong with Split()?

Eibro

#10
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 :(
Eibro of Yeti Lovers.

Etheran

#11
don't you mean center/centre color/colour ?

warz

#12
picky? more or less asking a stupid question...

Grok

#13
What is wrong with Split()?

Spht

So what's wrong with Split()?