New String Commands...

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

New String Commands...

Post by BackupUser »

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

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
Here you will find some string commands I coded.
[snip]
Don't forget that some of your commands are found in Mr Skunks Libaries:
http://www.skunknet.fr.st/


Edited by - PB on 20 September 2001 04:22:24
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi PB,
well the string2 library of Mr. Skunk has 3 commands.
The only command that has the same function is StripAll.
But IMHO the function behind StripAll is not logical, because
the 2 commands of PureBasic StripTrail and StripLead eliminates spaces NOT Strings.

So IMHO the command name Remove is for removing string quite better.
That's why I have build the command Strip (StripAll was alredy used) to remove leading and trailing spaces with one command.

When you take a look to BCX-Basic you will see, that there is the same command with the name Remove. I picked Remove because it was already established.

You can rename the command names whatever you want or like.
That's why I placed the sources in the forum.
Change the name of the procedure, and that's it.

BTW what if users make libraries with the same name but different functions?
How can PureBasic choose the right one?
Probably we need a list with occupied name for something like that.



Have a nice day...
Franco


Edited by - franco on 27 September 2001 16:22:50
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
Hi PB,
well the string2 library of Mr. Skunk has 3 commands.
The only command that has the same function is StripAll.
Ah, I see -- I didn't look into them too carefully.
Thanks for the info!


Edited by - PB on 20 September 2001 04:23:11
Post Reply