String matching: Difference between revisions

→‎{{header|PureBasic}}: Added alternate more complete solution
(→‎{{header|PureBasic}}: replace debug verification with console output)
(→‎{{header|PureBasic}}: Added alternate more complete solution)
Line 840:
CloseConsole()
EndIf</lang>
 
An alternate and more complete solution:
<lang PureBasic>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</lang>
Sample output:
<pre>1
0
 
0
1
3
7
0
 
0
1</pre>
 
=={{header|Powershell}}==
Anonymous user