String Function Library
What is it?
This is a Function Library I wrote to make your lives easier and to make up for the lack of string commands in DBP. There are various functions to replace words inside of strings, or to find where a word is and lots more.
What Functions are there?
Explode -
Explode a string into an array separated by characters.
GSub -
Substitute any words/letters within your string with other ones.
Find -
Find the position of a word/letter within your string.
Sub -
Get the substring of your string, remove any unwanted characters within a range.
Rep -
Repeat a string a designated amount.
Reverse -
Reverse a string.
Add/Remove -
Add or remove a designated amount of characters from a string where anywhere within it.
Trim -
Trim any trailing white space from either end of your string.
File Type -
Return the filetype from a path.
File Name -
Return the filename from a path.
File Path -
Return the file path from a path.
Examples?
yes the full source code to the library is below, and it comes with some basic examples showing you how to use the functions. If you use these functions in any of your projects please credit me.
`------------------------------------------
` String Library example
` By Carlos (Darkcoder) Wilkes
`------------------------------------------
`$$$$$$$$$$$$$$$$$$$$$$$$$ String_Explode example
String$ = "Look at this!,and,this=or=this" ` Create a string to explode
String_Explode( String$ , " =," ) ` Explode it
Text 0 , 0 , "Exploding: " + String$
For I = 0 To String_Length() ` Loop through the filled array
Text 0 , (I+2) * 10 , String_Row( I ) ` Print off values
Next I
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$ String GSub example
String$ = "Replace ME, ME ME ME!" ` Create a string to GSub
Return$ = String_GSub( String$ , "ME" , "YOU" ) ` Replace the word ME with YOU
Text 0 , 0 , String$
Text 0 , 10 , Return$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$ String reverse example
String$ = "Write me backwards!" ` Create a string to be reversed
Return$ = String_Reverse( String$ )
Text 0 , 0 , String$
Text 0 , 10 , Return$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$ String repeat
String$ = "Echo, " ` Make a string to repeat
Return$ = String_Rep( String$ , 10 ) ` Repeat it 10 times
Text 0 , 0 , String$
Text 0 , 10 , Return$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$ Substring
String$ = "!IGNORE! ME !IGNORE!" ` Make a string
Return$ = String_Sub( String$ , 9 , -9 ) ` Find the substring starts from 8 chars in, and 8 from the end
Text 0 , 0 , String$
Text 0 , 10 , Return$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$
String$ = " TRIM ME " ` Make a string with spaces
Return$ = String_Trim( String$ ) ` Trim all spaces at each end
Text 0 , 0 , String$
Text 0 , 10 , Return$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$
String$ = "Where am I hiding?" ` Make a string with 'I' in it
Found = String_Find( String$ , "I" ) ` Find the position of I
Text 0 , 0 , String$
Text 0 , 10 , "I is at positon: " + Str$( Found )
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$ File commands
String$ = "test123/folder1/filename.bmp" ` Make a filename string
Type$ = String_GetFileType( String$ )
Name$ = String_GetFileName( String$ )
Path$ = String_GetFilePath( String$ )
Text 0 , 0 , String$
Text 0 , 10 , "File Type = " + Type$
Text 0 , 20 , "File Name = " + Name$
Text 0 , 30 , "File Path = " + Path$
Wait Key
CLS
`$$$$$$$$$$$$$$$$$$$$$$$$$
END
` ##########################################################
`##############################################################
`### ###
`### DarkBASIC Pro String library ###
`### ###
`### By Carlos (Darkcoder) Wilkes ###
`### ###
`##############################################################
` ##########################################################
`############################################################
`############################################################
Function String_GSub( Lib_Input_String As String , Lib_Find_String As String , Lib_Replace_String As String )
Local Lib_Return_Value As String
If Lib_Replace_String = "" Or Lib_Find_String = ""
ExitFunction Lib_Input_String
Endif
For Lib_Scan = 1 To Len( Lib_Input_String )
If Mid$( Lib_Input_String , Lib_Scan ) = Mid$( Lib_Find_String , 1 )
For Lib_Search = 1 To Len( Lib_Find_String ) - 1
If Mid$( Lib_Input_String , Lib_Scan + Lib_Search ) = Mid$( Lib_Find_String , Lib_Search + 1 )
If Lib_Search = Len( Lib_Find_String ) - 1
Lib_Scan = Lib_Scan + Len( Lib_Find_String ) - 1
Lib_Return_Value = Lib_Return_Value + Lib_Replace_String
Endif
Else
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
Exit
Endif
Next Lib_Search
Else
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
Endif
Next Lib_Scan
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_GetFileType( Lib_Input_String As String )
Local Lib_Return_Value As String
Local Lib_Current_Buffer As String
Local Lib_Current_Mid As String
For Lib_Search = Len( Lib_Input_String ) To 1 Step -1
Lib_Current_Mid = Mid$( Lib_Input_String , Lib_Search )
If Lib_Current_Mid = "."
Lib_Current_Buffer = String_Reverse( Lib_Current_Buffer )
Exitfunction Lib_Current_Buffer
Else
If Lib_Current_Mid = "/" Or Lib_Current_Mid = ""
ExitFunction ""
Endif
Lib_Current_Buffer = Lib_Current_Buffer + Lib_Current_Mid
Endif
Next Lib_Search
EndFunction ""
`############################################################
`############################################################
Function String_GetFileName( Lib_Input_String As String )
Local Lib_Return_Value As String
Local Lib_Current_Buffer As String
Local Lib_Current_Mid As String
For Lib_Search = Len( Lib_Input_String ) To 1 Step -1
Lib_Current_Mid = Mid$( Lib_Input_String , Lib_Search )
If Lib_Current_Mid = "."
Lib_Current_Buffer = ""
Else
If Lib_Current_Mid = "/" Or Lib_Current_Mid = ""
Lib_Current_Buffer = String_Reverse( Lib_Current_Buffer )
ExitFunction Lib_Current_Buffer
Endif
Lib_Current_Buffer = Lib_Current_Buffer + Lib_Current_Mid
Endif
Next Lib_Search
EndFunction ""
`############################################################
`############################################################
Function String_GetFilePath( Lib_Input_String As String )
Local Lib_Return_Value As String
Local Lib_Current_Mid As String
For Lib_Search = Len( Lib_Input_String ) To 1 Step -1
Lib_Current_Mid = Mid$( Lib_Input_String , Lib_Search )
If Lib_Current_Mid = "/" Or Lib_Current_Mid = ""
Lib_Return_Value = Left$( Lib_Input_String , Lib_Search )
ExitFunction Lib_Return_Value
Endif
Next Lib_Search
EndFunction ""
`############################################################
`############################################################
Function String_Find( Lib_Input_String As String , Lib_Find_String As String )
Local Lib_Return_Value As Integer
If Lib_Find_String = ""
ExitFunction 0
Endif
For Lib_Scan = 1 To Len( Lib_Input_String )
If Mid$( Lib_Input_String , Lib_Scan ) = Mid$( Lib_Find_String , 1 )
If Len( Lib_Find_String ) > 1
For Lib_Search = 1 To Len( Lib_Find_String ) - 1
If Mid$( Lib_Input_String , Lib_Scan + Lib_Search ) = Mid$( Lib_Find_String , Lib_Search + 1 )
If Lib_Search = Len( Lib_Find_String ) - 1
ExitFunction Lib_Scan
Endif
Else
Exit
Endif
Next Lib_Search
Else
ExitFunction Lib_Scan
Endif
Endif
Next Lib_Scan
EndFunction 0
`############################################################
`############################################################
Function String_Trim( Lib_Input_String As String )
Local Lib_Return_Value As String
Local Lib_From As Integer
Local Lib_To As Integer
Lib_From = 1
Lib_To = Len( Lib_Input_String )
For Lib_Scan = 1 To Len( Lib_Input_String )
If Mid$( Lib_Input_String , Lib_Scan ) = " "
Inc Lib_From
Else
Exit
Endif
Next Lib_Scan
For Lib_Scan = Len( Lib_Input_String ) To 1 Step -1
If Mid$( Lib_Input_String , Lib_Scan ) = " "
Dec Lib_To
Else
Exit
Endif
Next Lib_Scan
For Lib_Loop = Lib_From To Lib_To
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Loop )
Next Lib_Loop
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Sub( Lib_Input_String As String , Lib_From As Integer , Lib_To As Integer )
Local Lib_Return_Value As String
If Lib_From < 0
Lib_From = Len( Lib_Input_String ) + Lib_From
Else
Inc Lib_From
Endif
If Lib_To < 0
Lib_To = Len( Lib_Input_String ) + Lib_To
Else
Inc Lib_To
Endif
For Lib_Scan = Lib_From To Lib_To
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
Next Lib_Scan
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Rep( Lib_Input_String As String , Lib_Iterations As Integer )
Local Lib_Return_Value As String
If Lib_Iterations
For Lib_Its = 1 To Lib_Iterations
Lib_Return_Value = Lib_Return_Value + Lib_Input_String
Next Lib_Its
Else
ExitFunction ""
Endif
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Reverse( Lib_Input_String As String )
Local Lib_Return_Value As String
For Lib_Scan = Len( Lib_Input_String ) To 1 Step -1
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
Next Lib_Scan
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Remove( Lib_Input_String As String , Lib_Position As Integer , Lib_Remove_Count As Integer )
Local Lib_Return_Value As String
For Lib_Scan = 1 To Len( Lib_Input_String )
If NOT Lib_Scan > Lib_Position And Lib_Scan < Lib_Position + Lib_Remove_Count
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
Endif
Next Lib_Scan
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Add( Lib_Input_String As String , Lib_Position As Integer , Lib_Add_String As String )
Local Lib_Return_Value As String
For Lib_Scan = 1 To Len( Lib_Input_String )
Lib_Return_Value = Lib_Return_Value + Mid$( Lib_Input_String , Lib_Scan )
If Lib_Scan = Lib_Position
Lib_Return_Value = Lib_Return_Value + Lib_Add_String
Endif
Next Lib_Scan
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Explode( Lib_Input_String As String , Lib_Separators As String )
Local Lib_Separator_Count As Integer
Local Lib_Current_Buffer As String
Local Lib_Current_Mid As String
Dim Lib_String(-1) As String
Dim Lib_Separator(-1) As String
If Lib_Input_String = "" Or Lib_Separators = ""
ExitFunction
Endif
For Lib_Sep = 1 To Len( Lib_Separators )
Inc Lib_Separator_Count
Array Insert At Bottom Lib_Separator(0)
Lib_Separator( Array Count(Lib_Separator(0)) ) = Mid$( Lib_Separators , Lib_Sep )
Next Lib_Sep
For Lib_Scan = 1 To Len( Lib_Input_String )
Lib_Current_Mid = Mid$( Lib_Input_String , Lib_Scan )
Lib_Current_Buffer = Lib_Current_Buffer + Lib_Current_Mid
For Lib_Sep = 0 To Array Count( Lib_Separator(0) )
If Lib_Current_Mid = Lib_Separator(Lib_Sep)
Array Insert At Bottom Lib_String(0)
Lib_String( Array Count( Lib_String(0) ) ) = Left$( Lib_Current_Buffer , Len( Lib_Current_Buffer ) - 1 )
Lib_Current_Buffer = ""
Exit
Endif
Next Lib_Sep
Next Lib_Scan
Array Insert At Bottom Lib_String(0)
Lib_String( Array Count( Lib_String(0) ) ) = Lib_Current_Buffer
EndFunction
`############################################################
`############################################################
Function String_Length()
Local Lib_Return_Value As Integer
Lib_Return_Value = Array Count( Lib_String(0) )
EndFunction Lib_Return_Value
`############################################################
`############################################################
Function String_Row( Lib_Row As Integer )
Local Lib_Return_Value As String
Lib_Return_Value = Lib_String( Lib_Row )
EndFunction Lib_Return_Value
`############################################################
`############################################################