Ooops
November 28, 2020, 01:57:07 PM

Author Topic: [bb] More versatile VB Split() Command by Jim Teeuwen [ 1+ years ago ]  (Read 608 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : More versatile VB Split() Command
Author : Jim Teeuwen
Posted : 1+ years ago

Description : This function Splits a string into a 1 dimensional Array called Split*() and let's you specify the delimiter and the number of results to return.

Perfect for converting an IP string into seperate numbers for instance.. There'll be lotsa other uses for it to tho..

All I want now is the possibility to do a Binary(case sensitive) or textcompare(case insensitive) between the source string and delimiter.. but I havnt figured that out yet..


Code :
Code: BlitzBasic
  1. [code]
  2. ;// Usage: bbSplit(Expression$ [, Delimiter$][, Count%])
  3. ;// Expression is needed (doh!).
  4.  
  5. ;// Delimiter(optional) is the character used to split the string.
  6. ;//  A blank Space is default.
  7.  
  8. ;// Count(optional) represents the number of items you want to have returned.
  9. ;//  Defaults to -1, wich means it will return all possible results.
  10.  
  11. ;// ### EXAMPLES #################################
  12.  
  13. ;// example 1.
  14. ;// Specify string
  15. mystring$="abcd,1234,beer,babes"
  16. ;// Split it and set ',' as the delimiter
  17. bbSplit(mystring$,",")
  18. ;// print the results
  19. print split(0)
  20. print split(1)
  21. print split(2)
  22. print split(3)
  23.  
  24. ;// output will be:
  25. abcd
  26. 1234
  27. beer
  28. babes
  29.  
  30. ;// example 2.
  31. ;// Specify string
  32. mystring$="123.456.789.0"
  33. ;// Split it and set '.' as the delimiter
  34. ;// this time we only want 2 results
  35. bbSplit(mystring$,".",2)
  36. ;// print the results
  37. print split(0)
  38. print split(1)
  39.  
  40. ;// output will be:
  41. 123
  42. 456
  43.  
  44. ;// ### THE GOODS ################################
  45.  
  46. Dim split(0)
  47. Type splitt
  48.         Field txt$
  49. End Type
  50.  
  51. Function bbsplit(txt$,devider$=" ",count=-1)
  52.         n=0
  53.         If Instr(txt$,devider$)=0 Then Goto e
  54.         Select count
  55.         Case -1
  56.                 While txt$<>""
  57.                         spl.splitt=New splitt
  58.                         If Instr(txt$,devider$)<>0 Then
  59.                                 spl     xt$=Left$(txt$,Instr(txt$,devider$)-1)
  60.                                 txt$=Mid$(txt$,Instr(txt$,devider$)+Len(devider$))
  61.                         Else
  62.                                 spl     xt$=txt$
  63.                                 txt$=""
  64.                         End If
  65.                 Wend
  66.         Default
  67.                 While txt$<>"" And n<count
  68.                         spl.splitt=New splitt
  69.                         If Instr(txt$,devider$)<>0 Then
  70.                                 spl     xt$=Left$(txt$,Instr(txt$,devider$)-1)
  71.                                 txt$=Mid$(txt$,Instr(txt$,devider$)+Len(devider$))
  72.                         Else
  73.                                 spl     xt$=txt$
  74.                         txt$=""
  75.                         End If
  76.                         n=n+1
  77.                 Wend
  78.         End Select
  79.         split_makeArray()
  80.         split_cleaner()
  81.         .e
  82. End Function
  83.  
  84. Function split_cleaner()
  85. Repeat
  86.         For spl.splitt=Each splitt
  87.                 Delete spl
  88.         Next
  89. Until spl=Null
  90. End Function
  91.  
  92. Function split_makeArray()
  93. j=0
  94. Repeat
  95.         For spl.splitt=Each splitt
  96.                 j=j+1
  97.         Next
  98. Until spl=Null
  99. Dim split(j)
  100. j=0
  101. Repeat
  102.         For spl.splitt=Each splitt
  103.                 split(j)=spl    xt$
  104.                 j=j+1
  105.         Next
  106. Until spl=Null
  107. End Function
  108.  
[/code]

Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal