75 lines
2.5 KiB
Text
75 lines
2.5 KiB
Text
|
|
Procedure StartsWith(String1$, String2$)
|
||
|
|
Protected Result
|
||
|
|
If FindString(String1$, String2$, 1) =1 ; E.g Found in possition 1
|
||
|
|
Result =CountString(String1$, String2$)
|
||
|
|
EndIf
|
||
|
|
ProcedureReturn Result
|
||
|
|
EndProcedure
|
||
|
|
|
||
|
|
Procedure EndsWith(String1$, String2$)
|
||
|
|
Protected Result, dl=Len(String1$)-Len(String2$)
|
||
|
|
If dl>=0 And Right(String1$, Len(String2$))=String2$
|
||
|
|
Result =CountString(String1$, String2$)
|
||
|
|
EndIf
|
||
|
|
ProcedureReturn Result
|
||
|
|
EndProcedure
|
||
|
|
|
||
|
|
; And a verification
|
||
|
|
|
||
|
|
If OpenConsole()
|
||
|
|
PrintN(Str(StartsWith("Rosettacode", "Rosetta"))) ; = 1
|
||
|
|
PrintN(Str(StartsWith("Rosettacode", "code"))) ; = 0
|
||
|
|
PrintN(Str(StartsWith("eleutherodactylus cruralis", "e"))) ; = 3
|
||
|
|
PrintN(Str(EndsWith ("Rosettacode", "Rosetta"))) ; = 0
|
||
|
|
PrintN(Str(EndsWith ("Rosettacode", "code"))) ; = 1
|
||
|
|
PrintN(Str(EndsWith ("Rosettacode", "e"))) ; = 2
|
||
|
|
|
||
|
|
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||
|
|
CloseConsole()
|
||
|
|
EndIf
|
||
|
|
|
||
|
|
; An alternate and more complete solution:
|
||
|
|
|
||
|
|
Procedure startsWith(string1$, string2$)
|
||
|
|
;returns one if string1$ starts with string2$, otherwise returns zero
|
||
|
|
If FindString(string1$, string2$, 1) = 1
|
||
|
|
ProcedureReturn 1
|
||
|
|
EndIf
|
||
|
|
ProcedureReturn 0
|
||
|
|
EndProcedure
|
||
|
|
|
||
|
|
Procedure contains(string1$, string2$, location = 0)
|
||
|
|
;returns the location of the next occurrence of string2$ in string1$ starting from location,
|
||
|
|
;or zero if no remaining occurrences of string2$ are found in string1$
|
||
|
|
ProcedureReturn FindString(string1$, string2$, location + 1)
|
||
|
|
EndProcedure
|
||
|
|
|
||
|
|
Procedure endsWith(string1$, string2$)
|
||
|
|
;returns one if string1$ ends with string2$, otherwise returns zero
|
||
|
|
Protected ls = Len(string2$)
|
||
|
|
If Len(string1$) - ls >= 0 And Right(string1$, ls) = string2$
|
||
|
|
ProcedureReturn 1
|
||
|
|
EndIf
|
||
|
|
ProcedureReturn 0
|
||
|
|
EndProcedure
|
||
|
|
|
||
|
|
If OpenConsole()
|
||
|
|
PrintN(Str(startsWith("RosettaCode", "Rosetta"))) ; = 1, true
|
||
|
|
PrintN(Str(startsWith("RosettaCode", "Code"))) ; = 0, false
|
||
|
|
|
||
|
|
PrintN("")
|
||
|
|
PrintN(Str(contains("RosettaCode", "luck"))) ; = 0, no occurrences
|
||
|
|
Define location
|
||
|
|
Repeat
|
||
|
|
location = contains("eleutherodactylus cruralis", "e", location)
|
||
|
|
PrintN(Str(location)) ;display each occurrence: 1, 3, 7, & 0 (no more occurrences)
|
||
|
|
Until location = 0
|
||
|
|
|
||
|
|
PrintN("")
|
||
|
|
PrintN(Str(endsWith ("RosettaCode", "Rosetta"))) ; = 0, false
|
||
|
|
PrintN(Str(endsWith ("RosettaCode", "Code"))) ; = 1, true
|
||
|
|
|
||
|
|
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||
|
|
CloseConsole()
|
||
|
|
EndIf
|