simple String function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Motu
Enthusiast
Enthusiast
Posts: 160
Joined: Tue Oct 19, 2004 12:24 pm

simple String function

Post by Motu »

Something I need very often when operating with files is a split or seperate function like this one:

Code: Select all

; Will Seperate a String
Procedure.s Seperate(String.s,Seperator.s,Index.l,Trim.l)
 Define Count.l, Start.l, n.l
 
 If Right(String,Len(Seperator)) <> Seperator
  String = String + Seperator
 EndIf
 For n = 1 To Len(String)
  If Mid(String,n,Len(Seperator)) = Seperator
   count + 1
   If count = index
    String = Left(String,n-1)
    If Trim = 1
     ProcedureReturn ReplaceString(String," ","")
    Else
     ProcedureReturn String
    EndIf
   Else
    String = Right(String,Len(String)-n)
    n = 0
   EndIf
  EndIf
 Next
EndProcedure
; End Seperate(String.s,Separator.s,Index.l)
It seperates a String by a seperator and optional removes fill characters (not needed very often)

for example

Code: Select all

debug Seperate("Martin;Winterhuder Weg 82;someone@something.com",";",1,0) ; Returns 'Martin'
debug Seperate("Martin;Winterhuder Weg 82;someone@something.com",";",2,0) ; Returns 'Winterhuder Weg 82'
debug Seperate("Martin;Winterhuder Weg 82;someone@something.com",";",2,1) ; Returns 'WinterhuderWeg82'
Maybe it can be added to the string lib native for all the other users to enjoy it ;)
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: simple String function

Post by TomS »

This is already implemented (without the "Space removal tool") ;-)

Code: Select all

Debug StringField("Martin;Winterhuder Weg 82;someone@something.com",1,";")
Debug StringField("Martin;Winterhuder Weg 82;someone@something.com",2,";")
Post Reply