March 05, 2021, 06:36:22 AM

Author Topic: [bb] SplitString function - Splits a string into fields and returns it in an array by Zethrax [ 1+ years ago ]  (Read 473 times)

Offline BlitzBot

Title : SplitString function - Splits a string into fields and returns it in an array
Author : Zethrax
Posted : 1+ years ago

Description : * Blitz3D code. May also work with BlitzPlus. *

This function takes a string value and splits it into fields using a delimiter string. The resulting fields are returned in an array.

Splits the specified stringval$ into fields using the div$ string as the delimiter. The resulting string fields are placed into the A_string$() array.

The div$ string can contain multiple characters. If it is empty then the array will have one element which will contain the entire string.

The index of the last array slot is returned by the function.

The max_elements parameter can be used to set a limit on the number of fields the string is split into. For no element limits, use a zero value for max_elements (default).

When max_elements or the end of the string is reached, the remainder of the string is returned in the last array element. The returned array will contain at least one element even if there is no divider string found.


Code :
Code: BlitzBasic
  1. Dim A_string$( 0 )
  2.  
  3. Function SplitString( stringval$, div$, max_elements = 0 )
  4.  
  5.         ; Splits the specified stringval$ into fields using the div$
  6.         ; string as the delimiter. The resulting string fields are
  7.         ; placed into the A_string$() array.
  8.        
  9.         ; The div$ string can contain multiple characters. If it is
  10.         ; empty then the array will have one element which will
  11.         ; contain the entire string.
  12.        
  13.         ; The index of the last array slot is returned by the function.
  14.        
  15.         ; The max_elements parameter can be used to set a limit on
  16.         ; the number of fields the string is split into. For no
  17.         ; element limits, use a zero value for max_elements (default).
  18.        
  19.         ; When max_elements or the end of the string is reached, the
  20.         ; remainder of the string is returned in the last array element.
  21.         ; The returned array will contain at least one element even if
  22.         ; there is no divider string found.
  23.        
  24.         If div$ = ""
  25.                 Dim A_string$( 0 )
  26.                 A_string$( 0 ) = stringval$
  27.                 Return 0
  28.         EndIf
  29.  
  30.         Local divlen = Len( div$ )
  31.         Local x = 0, startpos = 1, divpos = 1
  32.  
  33.         Repeat
  34.                 divpos = Instr( stringval$, div$, divpos )
  35.                 If divpos
  36.                         x = x + 1
  37.                         divpos = divpos + divlen
  38.                 EndIf
  39.         Until divpos = 0
  40.  
  41.         If max_elements
  42.                 If max_elements < x Then x = max_elements
  43.         EndIf
  44.  
  45.         Dim A_string$( x )
  46.  
  47.         max_elements = max_elements - 1
  48.  
  49.         x = 0
  50.  
  51.         While x <> max_elements
  52.                 divpos = Instr( stringval$, div$, startpos )
  53.                 If divpos = 0 Then Exit
  54.                 A_string$(x) = Mid( stringval$, startpos, divpos - startpos )
  55.                 startpos = divpos + divlen
  56.                 x = x + 1
  57.         Wend
  58.         A_string$(x) = Right( stringval$, Len( stringval$ ) - startpos + 1 )
  59.         Return x
  60. End Function
  61.  
  62. ; *** EXAMPLE CODE ***
  63.  
  64. max_index = SplitString( "aa<!>bb<!>cc<!>dd<!>ee<!>", "<!>", 0 )
  65.  
  66. Print "Highest Index: " + max_index
  67. Print
  68.  
  69. For i = 0 To max_index
  70.         Print "["+A_string$( i )+"]"
  71. Next
  72.  
  73. Print
  74. Print "Press any key to exit."
  75. WaitKey
  76. End


Comments :


TaskMaster(Posted 1+ years ago)

 isn't there a split function already in BlitzMax?


Yasha(Posted 1+ years ago)

 There is, but this is for Blitz Basic, where there is not.There are a few others already in the archives, but this is a nice clear implementation by the look of it.


laughing_man(Posted 1+ years ago)

 thanks


Kippykip(Posted 1+ years ago)

 Works great


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal