The following functions determine what to return depending on whether a string starts with something, or ends with something.
The first pair of functions return true if the string, starts or ends a series of characters. An additional parameter allows you to change the character case mode. Setting it to 1 will require the string to be exact; otherwise lower or upper cased versions of the same character will match. EndsWith$("My name is Sam", "Sam", 0) returns true; EndsWith$("1234", "4", 0) returns true; StartsWith$("1234","4",0) returns false.
The second pair of functions return a new string ending or starting with a required pattern, whether or not the pattern is not already there. For example: RequireSuffix$("file", ".x", ".dbo") returns 'file.x' because the first parameter does not already end with the required suffix. The function provides an additional default parameter for using a dynamic expression to determine the extension. RequireSuffix$("file", GetRequiredFileExtension(), ".txt") will suffix the dynamic extension provided by the anonymous function GetRequiredFileExtension(), if the function returns no suffix, the default suffix '.txt' is appended instead. RequirePrefix$() is the reverse version of RequireSuffix$() which appends at the start instead of the end, therefore RequirePrefix$("file.txt","c:\program files\game\, "") will return "c:\program files\game\file.txt".
Matrix1
//================================================================
Function EndsWith(sText$, sEnd$, iCaseSensitive)
If sText$ <> ""
l = Fast Len(sEnd$)
If Fast Right$( sText$, l ) = sEnd$ Then ExitFunction 1
If iCaseSensitive = 0
If Fast Right$( Fast Lower$(sText$), l ) = Fast Lower$(sEnd$) Then ExitFunction 1
Endif
Endif
Endfunction 0
//=======================================================
Function StartsWith(sText$, sWith$, bCaseSensitive)
If bCaseSensitive
If Fast Left$( sText$, Fast Len(sWith$) ) = sWith$
ExitFunction 1
Endif
Else
If Fast Left$( Fast Lower$(sText$), Fast Len(sWith$) ) = Fast Lower$(sWith$)
ExitFunction 1
Endif
Endif
Endfunction 0
//================================================================
Function RequireSuffix$( sString$, sSuffix$, sOrOtherSuffix$ )
s$ = Fast Lower$(sString$)
If sSuffix$ <> ""
l = Fast Len( sSuffix$ )
If Fast Right$(s$, l) <> Fast Lower$(sSuffix$)
sString$ = sString$ + sSuffix$
Endif
Else
If sOrOtherSuffix$ <> ""
l = Fast Len( sOrOtherSuffix$ )
If Fast Right$(s$, l) <> Fast Lower$(sOrOtherSuffix$)
sString$ = sString$ + sOrOtherSuffix$
Endif
EndIf
EndIf
Endfunction sString$
//================================================================
Function RequirePrefix$( sString$, sPrefix$, sOrOtherPrefix$ )
s$ = Fast Lower$(sString$)
If sPrefix$ <> ""
l = Fast Len( sPrefix$ )
If Fast Left$(s$, l) <> Fast Lower$(sPrefix$)
sString$ = sPrefix$ + sString$
Endif
Else
If sOrOtherPrefix$ <> ""
l = Fast Len( sOrOtherPrefix$ )
If Fast Left$(s$, l) <> Fast Lower$(sOrOtherPrefix$)
sString$ = sOrOtherPrefix$ + sString$
Endif
EndIf
EndIf
Endfunction sString$
Vanilla
//================================================================
Function EndsWith(sText$, sEnd$, iCaseSensitive)
If sText$ <> ""
l = Len(sEnd$)
If Right$( sText$, l ) = sEnd$ Then ExitFunction 1
If iCaseSensitive = 0
If Fast Right$( Lower$(sText$), l ) = Lower$(sEnd$) Then ExitFunction 1
Endif
Endif
Endfunction 0
//=======================================================
Function StartsWith(sText$, sWith$, bCaseSensitive)
If bCaseSensitive
If Left$( sText$, Len(sWith$) ) = sWith$
ExitFunction 1
Endif
Else
If Left$( Lower$(sText$), Fast Len(sWith$) ) = Lower$(sWith$)
ExitFunction 1
Endif
Endif
Endfunction 0
//================================================================
Function RequireSuffix$( sString$, sSuffix$, sOrOtherSuffix$ )
s$ = Lower$(sString$)
If sSuffix$ <> ""
l = Fast Len( sSuffix$ )
If Right$(s$, l) <> Lower$(sSuffix$)
sString$ = sString$ + sSuffix$
Endif
Else
If sOrOtherSuffix$ <> ""
l = Fast Len( sOrOtherSuffix$ )
If Right$(s$, l) <> Lower$(sOrOtherSuffix$)
sString$ = sString$ + sOrOtherSuffix$
Endif
EndIf
EndIf
Endfunction sString$
//================================================================
Function RequirePrefix$( sString$, sPrefix$, sOrOtherPrefix$ )
s$ = Lower$(sString$)
If sPrefix$ <> ""
l = Fast Len( sPrefix$ )
If Left$(s$, l) <> Lower$(sPrefix$)
sString$ = sPrefix$ + sString$
Endif
Else
If sOrOtherPrefix$ <> ""
l = Fast Len( sOrOtherPrefix$ )
If Left$(s$, l) <> Lower$(sOrOtherPrefix$)
sString$ = sOrOtherPrefix$ + sString$
Endif
EndIf
EndIf
Endfunction sString$