Split Function

BlitzMax Forums/BlitzMax Beginners Area/Split Function

Sloan(Posted 2005) [#1]
First time poster, long time user. Well, of Blitz. First time using BlitzMax and I see there is still no split function :o). Here is one for anyone interested. I needed it for a platform game I'm working on. Use. Enjoy. Etc.

Type TStringUtil

Field stringy:String

Method Split:Array(splitter:String)
Local sa:String[]
Local pos:Int = 1
Local lastpos:Int = 1
Local lastString:String
Local count:Int = 0

pos = stringy.Find(splitter, pos)
While pos < Len(stringy) And pos > 0
lastString = Mid(stringy, lastpos, (pos-lastpos)+1)
pos = pos + 1
lastpos = pos + 1
pos = stringy.Find(splitter, pos)
sa = sa[..count+1]
sa[count] = lastString
count = count + 1
Wend
sa = sa[..count+1]
sa[count] = Right(stringy, Len(stringy)-lastpos+1)
Return sa

End Method

Function Create:TStringUtil(s:String)
a:TStringUtil = New TStringUtil
a.stringy = s
Return a
End Function

End Type

You can used this sample bit of code here to get started:

t:TStringUtil = TStringUtil.Create("11,2,33,44")
Local s:String[]
s = t.Split(",")
Print s[1]

Cheers,
Sloan


Bot Builder(Posted 2005) [#2]
Welcome to the boards.

Lol. When I saw this topic I was like :O because of two things:

A. Just finished a split function for strings yssterday: http://www.blitzbasic.com/Community/posts.php?topic=42495

B. Your nick is my last name :O

Anyway, a good leightweight splitter. Go to my link if you need a heavyweight ;)


Sloan(Posted 2005) [#3]
Thanks Bot Builder! Now, sing with me: "It's a small, small world..." :o) I'll take your splitter for a spin later tonight.


Robert(Posted 2005) [#4]
Already written a module for this! (See the RobK.StringUtils thread in the Module Tweaks forum)


Sloan(Posted 2005) [#5]
D'oh! For completeness the link is here to Rob's string functions (inc. split()) -> http://www.blitzmax.com/Community/posts.php?topic=42540. First rule of software development and I've broken it: Never re-invent the wheel. Ach well... it's in the Beginners Forum for all to see !