Easy Split and Join Strings
Hi this is an example of doing strings in VB to split and join strings, I know there is aready functions to do this, but i wanted to have a go at makeing my own, anyway hope they be usfull
Place the code below into the general part of your form
Code:
Function vJoin(ByVal SourceArray, JoinChar As String) As String
Dim Count As Long
Dim Buff As String
Do While (Count < UBound(SourceArray))
'Build the string
Buff = Buff & SourceArray(Count) & JoinChar
'Inc count
Count = (Count + 1)
Loop
'Remove last JoinChar
Buff = Left$(Buff, Len(Buff) - 1)
vJoin = Buff
'Clear up time
Buff = vbNullString
End Function
Function vSplit(ByVal Expression As String, Optional SplitChar As String = ",") As Variant
Dim Pos As Long
Dim Count As Long
Dim Inc As Long
Dim Temp() As String
Count = 1
Do While (Count < Len(Expression))
'Find the position of SplitChar within Expression
Pos = InStr(Count, Expression, SplitChar, vbTextCompare)
'If we are at zero set pos to the length of the string + 1 to make sure we get the last string
If (Pos = 0) Then Pos = Len(Expression) + 1
'Resize array to hold strings
ReDim Preserve Temp(Inc) As String
'Store strings into array
Temp(Inc) = Mid$(Expression, Count, Pos - Count)
'Inc array counter
Inc = (Inc + 1)
'Inc counter
Count = Pos + Len(SplitChar)
Loop
'Return split strings
vSplit = Temp
End Function
Example place a new command button on the form and add the code below:
Code:
Dim Test() As String
Dim X As Long
'Example
Test = vSplit("Software,Games,Hardware,Split,Test", ",")
'Show split strings
For X = 0 To UBound(Test)
MsgBox Test(X)
Next X
'Rejoin the array and show result
MsgBox vJoin(Test, ",")