Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation / Array Helper

Status
Not open for further replies.

DreXor

Programmer
Jun 17, 2003
2,224
US
If to be used IN ASP pages you'll need to strip this down alittle unless you use option explicit ( save for the optional arguments )

If you've ever worked with repeatative arrays, with multiple delimiters... split, then split again inside a loop to try and get everything separated, this might help :


Function ReSplit(OriginArray, sDelimiter As String, Optional CompareMethod As Long = vbBinaryCompare) As Variant
If IsArray(OriginArray) Then
Dim OutputArray() As String, iArrayUpper As Integer, FirstInSet As Integer, NewUbound As Integer
NewUbound = -1
For ArrayPosition = 0 To UBound(OriginArray)
NewUbound = NewUbound + 1
SubString = OriginArray(ArrayPosition)
FirstInSet = InStr(1, SubString, sDelimiter, CompareMethod)
Do While FirstInSet > 0
ReDim Preserve OutputArray(NewUbound)
OutputArray(NewUbound) = Left(SubString, FirstInSet - 1)
SubString = Right(SubString, Len(SubString) - FirstInSet)
FirstInSet = InStr(1, SubString, sDelimiter, CompareMethod)
NewUbound = NewUbound + 1
Loop
ReDim Preserve OutputArray(NewUbound)
OutputArray(NewUbound) = SubString
Next
ReSplit = OutputArray
Else
ReSplit = Array("Error")
End If
End Function



Sample Usage :

dim blah()

blah = Split("a,b,c,d,f!g!h!i,j,k,l",",")
' pretty straight foreward, (8 spots (7 dim)) ..but you don't have a broken down full group of each letter so....
blah = ReSplit(blah,"!")
' this reconstructs the blah array into one full array (11 spots (10 dim))

can come in very handy in string manipulations, sorting out large masses of text, second example : web pages...

dim blah()

blah = split(webcontent,"<")
blah = ReSplit(blah,">")

' this results in every other item in the array to alternate between tag content and page content, even if it's empty like <table><tr>
 
Update!!!

forgot to count in the length of the "delimiter" in case it's more than a single character.

updated function :

Code:
Function ReSplit(OriginArray, sDelimiter As String, Optional CompareMethod As Long = vbBinaryCompare) As Variant
  If IsArray(OriginArray) Then
    Dim OutputArray() As String, iArrayUpper As Integer, FirstInSet As Integer, NewUbound As Integer
    NewUbound = -1
    For ArrayPosition = 0 To UBound(OriginArray)
      NewUbound = NewUbound + 1
      SubString = OriginArray(ArrayPosition)
      FirstInSet = InStr(1, SubString, sDelimiter, CompareMethod)
      Do While FirstInSet > 0
        ReDim Preserve OutputArray(NewUbound)
        OutputArray(NewUbound) = Left(SubString, FirstInSet - 1)
        SubString = Right(SubString, Len(SubString) - FirstInSet[COLOR=black cyan] - Len(sDelimiter)[/color])
        FirstInSet = InStr(1, SubString, sDelimiter, CompareMethod)
        NewUbound = NewUbound + 1
      Loop
      ReDim Preserve OutputArray(NewUbound)
      OutputArray(NewUbound) = SubString
    Next
    ReSplit = OutputArray
  Else
    ReSplit = Array("Error")
  End If
End Function

[thumbsup2]DreX
aKa - Robert
 
Hi Robert, I was interested in trying this out but I get the following error:

Expected ')'

/code/case_for_calculations.asp, line 80

Function ReSplit(OriginArray, sDelimiter As String, Optional CompareMethod As Long = vbBinaryCompare) As Variant
-----------------------------------------^

The arrow is actually pointing under the As in sDelimeter As String.

Any ideas? Thanks!!
 
Drexor said:
If to be used IN ASP pages you'll need to strip this down a little unless you use option explicit ( save for the optional arguments )

It's written as VB code not vbScript so the AS and optional statements need removing



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Oh my gosh, thanks Chris. I was so excited about using it I completely missed that. [blush] Thanks again!
 
Um...I'm not sure I am understanding, you have a string with two differant delimiters and you want a single array? Why not just use replace?
Code:
Function SplitMultiple(strContent,arrOfDelims)
   Dim delim, newContent
   newContent = strContent
   For Each delim in arrOfDelims
      Replace(newContent,delim,"###ARRDELIM###")
   Next

   SplitMultiple = Split(newContent,"###ARRDELIM###")
End Function

'example
blah = "a,b,c,d,f!g!h!i,j,k,l"
array_of_blah = SplitMultiple(blah,Array(",","!"))

'or
array_of_html = SplitMultiple(yourHTML,Array("<",">"))

Sorry, that way just seemed more obvious to me, though yours may be more efficient, guess it would depend on the number of delimiters, length of the string, and size of the array generated from the firt delimiter.

-T


01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top