New String Commands...
Posted: Fri Aug 17, 2001 10:21 pm
Restored from previous forum. Originally posted by Franco.
Here you will find some string commands I coded.
Hope somebody can use it... (...and they work well!)
Franco
Have a nice day...
Franco
Here you will find some string commands I coded.
Hope somebody can use it... (...and they work well!)
Franco
Code: Select all
; String function CountWords
; Returns the number of words in a string
; You can declare the divide char to localize the words.
; something like: CountWords("Hello,World",",") will return 2
; something like: CountWords("Hello,World,",",") will return 3
; because the third word is empty but separated!
; something like: CountWords("Hello,nice,World",",") will return 3
; something like: CountWords("Hello,,World",",") will return 3
; because the second word is empty but separated! and so on...
; usage: CountWords(String$,Divider$)
Procedure CountWords(S$,D$)
If Len(S$)=0
W=0
Else
W=1 : F=1
While FindString(S$,D$,F)>0
F=FindString(S$,D$,F)+1
W=W+1
Wend
EndIf
ProcedureReturn W
EndProcedure
; String function Delete
; Deletes a given lenght of characters in a string from a given position
; Usage: Delete(String$,StartPosition,Lenght)
Procedure$ Delete(S$,P,L)
If P>0 And L>0
S$=Left(S$,P-1)+Right(S$,Len(S$)-(P+L-1))
EndIf
ProcedureReturn S$
EndProcedure
; String function Insert
; Inserts a string to a string from a given position
; If the position is higher then the lenght of the string it will be added
; Usage: Insert(String$,InsertString$,StartPosition)
Procedure$ Insert(S$,I$,P)
While Len(S$)0
S$=Left(S$,P-1)+I$+Right(S$,Len(S$)-(P-1))
EndIf
ProcedureReturn S$
EndProcedure
; String function MCase
; Returns the original string converted in to mixed case.
; The string starts with upper case.
; Usage: MCase(String$)
Procedure$ MCase(S$)
ProcedureReturn UCase(Left(S$,1))+LCase(Right(S$,Len(S$)-1))
EndProcedure
; String function Remove
; Removes all occurrences of a string in a string.
; Usage: Remove(String$,RemoveString$)
Procedure$ Remove(S$,R$)
F=1
While FindString(S$,R$,F)>0
S$=Left(S$,FindString(S$,R$,F)-1)+Right(S$,Len(S$)-(FindString(S$,R$,F)+Len(R$)-1))
F=F+1
Wend
ProcedureReturn S$
EndProcedure
; String function Replace
; Replaces all occurrences of 'replacestring' with 'newstring'.
; Usage: Replace(String$,ReplaceString$,NewString$)
Procedure$ Replace(S$,R$,N$)
While FindString(S$,R$,1)>0
S$=Left(S$,(FindString(S$,R$,1)-1))+N$+Right(S$,Len(S$)-(FindString(S$,R$,1)+Len(R$)-1))
Wend
ProcedureReturn S$
EndProcedure
; String function ReturnWord
; Returns a word with a given number from a string.
; You can declare the divide char to localize the words.
; Usage: ReturnWord(String$,Divider$,WordPosition)
Procedure$ ReturnWord(S$,D$,P)
If Len(S$)=0
W=0
Else
W=1 : F=1
While FindString(S$,D$,F)>0
F=FindString(S$,D$,F)+1
W=W+1
Wend
If W0 : B=1 : L=F-1 : EndIf
If P>1 And F>0 And W=P : L=F-B : EndIf
If P>1 And F=0 And W=P : L=Len(S$)-B+1 : EndIf
If P>1 And F>0 And W0
O$=O$+Mid(S$,P,1)
P=P-1
Wend
ProcedureReturn O$
EndProcedure
; String function Strip
; Removes all the 'space' characters located in front and at the end of a string.
; Usage: Strip(String$)
Procedure$ Strip(S$)
ProcedureReturn StripLead(StripTrail(S$))
EndProcedure
Text$ = Str(CountWords("Hello World"," "))
MessageBox_(0,Text$,"CountWords: All is OK",0)
Text$ = Delete("Hello World !",10,1)
MessageBox_(0,Text$,"Delete: All is OK",0)
Text$ = Insert("Hello World !","nice ",7)
MessageBox_(0,Text$,"Insert: All is OK",0)
Text$ = MCase("hElLO!")
MessageBox_(0,Text$,"MCase: All is OK",0)
Text$ = Remove("Hello nice World !","nice ")
MessageBox_(0,Text$,"Remove: All is OK",0)
Text$ = Replace("Hello World World !","World","Franco")
MessageBox_(0,Text$,"Replace: All is OK",0)
Text$ = ReturnWord("Hello,nice,World,!",",",3)
MessageBox_(0,Text$,"ReturnWord: All is OK",0)
Text$ = Reverse("Hello World !")
MessageBox_(0,Text$,"Reverse: All is OK",0)
Text$ = Strip(" Hello World ! ")
MessageBox_(0,Text$,"Strip: All is OK",0)
End
Have a nice day...
Franco