Do you guys have a better Split() function?

Post

Posted
Rating:
#1 (In Topic #1561)
Regular
JumpyVB is in the usergroup ‘Regular’
The built-in Split() function only seems to accept a single character as the delimiter. Has any one of you already made a better version that supports longer delimiter strings, and also behaves nicely with UTF-8 strings?
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Split takes multiple characters but they are treated singularly.

Ie, Split(sString, " \t\r\n")
will split at any individual space, tab, CR or LF

there is not a way to split at a "word"
A quick way to do it is to use Replace and replace your delimiter text with a single unlikely to be used char like ®

Code (gambas)

  1.  
  2. Public Sub SplitAtWords(Text As String, Delimiter As String, Optional Escape As String, RemoveBlanks As Boolean) As String[]
  3.  
  4.   Dim sOddChar As String = "®"
  5.   Dim aStr As String[]
  6.   If Not InStr(Text, Delimiter) then return [Text]  ' delimiter not found so return the string whole
  7.  
  8.   aStr = Split(Replace(Text, Delimiter, aOddChar), sOddChar, Escape, RemoveBlanks)  ' replace search string for ® and split at ®
  9.  
  10.   Return aStr
  11.  
  12.  
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
JumpyVB is in the usergroup ‘Regular’
Ah. I see what you did there, and I like your thinking. Thank you for the idea. This works for me.
Online now: No Back to the top
1 guest and 0 members have just viewed this.