String matching: Difference between revisions

Content added Content deleted
(add language: Retro)
m (curation)
Line 8: Line 8:


Optional requirements:
Optional requirements:
A) Print the location of the match for part 2
# Print the location of the match for part 2
B) Handle multiple occurrences of a string for part 2.
# Handle multiple occurrences of a string for part 2.

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{trans|python}}
{{trans|python}}

{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}

{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - string in string is non-standard?}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - string in string is non-standard?}}
Line 39: Line 38:
TFFT -1 +1 +3
TFFT -1 +1 +3
</pre>
</pre>

=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <string>

<lang cpp>
#include <string>
using namespace std;
using namespace std;


Line 55: Line 53:
s1.find(s2)//returns string::npos
s1.find(s2)//returns string::npos
int loc=s2.find(s3)//returns 0
int loc=s2.find(s3)//returns 0
loc=s2.find(s3,loc+1)//returns 2
loc=s2.find(s3,loc+1)//returns 2</lang>
</lang>


=={{header|J}}==
=={{header|J}}==

<lang j>startswith=: ] -: ({.~ #)
<lang j>startswith=: ] -: ({.~ #)
contains=: +./@:E.~
contains=: +./@:E.~
Line 94: Line 90:


=={{header|Java}}==
=={{header|Java}}==
<lang java>"abcd".startsWith("ab") //returns true

<lang java>
"abcd".startsWith("ab") //returns true
"abcd".endsWith("zn") //returns false
"abcd".endsWith("zn") //returns false
"abab".contains("bb") //returns false
"abab".contains("bb") //returns false
Line 102: Line 96:
int loc = "abab".indexOf("bb") //returns -1
int loc = "abab".indexOf("bb") //returns -1
loc = "abab".indexOf("ab") //returns 0
loc = "abab".indexOf("ab") //returns 0
loc = "abab".indexOf("ab",loc+1) //returns 2
loc = "abab".indexOf("ab",loc+1) //returns 2</lang>
</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
<lang objc>[@"abcd" hasPrefix:@"ab"] //returns true

<lang objc>
[@"abcd" hasPrefix:@"ab"] //returns true
[@"abcd" hasSuffix:@"zn"] //returns false
[@"abcd" hasSuffix:@"zn"] //returns false
int loc = [@"abab" rangeOfString:@"bb"].location //returns -1
int loc = [@"abab" rangeOfString:@"bb"].location //returns -1
loc = [@"abab" rangeOfString:@"ab"].location //returns 0
loc = [@"abab" rangeOfString:@"ab"].location //returns 0
loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2
loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2</lang>
</lang>
=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure StartsWith(String1$, String2$)
<lang PureBasic>Procedure StartsWith(String1$, String2$)
Line 139: Line 129:


=={{header|Python}}==
=={{header|Python}}==
<lang python>"abcd".startswith("ab") #returns true

<lang python>
"abcd".startswith("ab") #returns true
"abcd".endswith("zn") #returns false
"abcd".endswith("zn") #returns false
"bb" in "abab" #returns false
"bb" in "abab" #returns false
Line 147: Line 135:
loc = "abab".find("bb") #returns -1
loc = "abab".find("bb") #returns -1
loc = "abab".find("ab") #returns 0
loc = "abab".find("ab") #returns 0
loc = "abab".find("ab",loc+1) #returns 2
loc = "abab".find("ab",loc+1) #returns 2</lang>
</lang>


=={{header|Retro}}==
=={{header|Retro}}==

<lang Retro>with strings'
<lang Retro>with strings'