String matching: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Perl 6}}: multiple matches with 'indices')
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 62:
CD
</pre>
 
 
=={{header|Ada}}==
Line 268 ⟶ 267:
{{Out}}
<pre>{8, 44, 83, 110}</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 603:
}
</lang>
 
 
=={{header|BASIC}}==
Line 744 ⟶ 743:
 
Press any key to continue . . .</pre>
 
 
=={{header|BBC BASIC}}==
Line 908 ⟶ 906:
matching `something random' with `Rand':
end match</pre>
 
=={{header|C++}}==
<lang cpp>#include <string>
using namespace std;
 
string s1="abcd";
string s2="abab";
string s3="ab";
//Beginning
s1.compare(0,s3.size(),s3)==0;
//End
s1.compare(s1.size()-s3.size(),s3.size(),s3)==0;
//Anywhere
s1.find(s2)//returns string::npos
int loc=s2.find(s3)//returns 0
loc=s2.find(s3,loc+1)//returns 2</lang>
 
=={{header|C sharp|C#}}==
Line 942 ⟶ 924:
}
</lang>
 
=={{header|C++}}==
<lang cpp>#include <string>
using namespace std;
 
string s1="abcd";
string s2="abab";
string s3="ab";
//Beginning
s1.compare(0,s3.size(),s3)==0;
//End
s1.compare(s1.size()-s3.size(),s3.size(),s3)==0;
//Anywhere
s1.find(s2)//returns string::npos
int loc=s2.find(s3)//returns 0
loc=s2.find(s3,loc+1)//returns 2</lang>
 
=={{header|Clojure}}==
Line 1,181 ⟶ 1,179:
2
1</pre>
 
=={{header|DCL}}==
<lang DCL>$ first_string = p1
Line 1,407 ⟶ 1,406:
Acc.
</lang>
 
=={{header|Euphoria}}==
<lang euphoria>sequence first, second
Line 2,061:
In fact, it happens 2 times within 'tacoloco', at indexes 2, 6.
3: Does 'tacoloco' end with 'co'? Yes.</pre>
 
 
=={{header|jq}}==
Line 2,100 ⟶ 2,099:
1
5</lang>
 
 
=={{header|Julia}}==
Line 2,134 ⟶ 2,132:
_ss["abcdabceabc";"abc"] / location of matches
0 4 8</lang>
 
 
=={{header|Kotlin}}==
Line 2,162 ⟶ 2,159:
[[File:LabVIEW_Character_matching_2.png]]<br/>
[[File:LabVIEW_Character_matching_3.png]]
 
 
=={{header|Lasso}}==
 
<lang Lasso>local(
a = 'a quick brown peanut jumped over a quick brown fox',
b = 'a quick brown'
)
 
//Determining if the first string starts with second string
#a->beginswith(#b) // true
 
//Determining if the first string contains the second string at any location
#a >> #b // true
#a->contains(#b) // true
 
//Determining if the first string ends with the second string
#a->endswith(#b) // false</lang>
 
=={{header|Lang5}}==
Line 2,202 ⟶ 2,181:
"rosettacode" "edoc" end-with . # 0
"rosettacode" "code" contains . # 7</lang>
 
=={{header|Lasso}}==
 
<lang Lasso>local(
a = 'a quick brown peanut jumped over a quick brown fox',
b = 'a quick brown'
)
 
//Determining if the first string starts with second string
#a->beginswith(#b) // true
 
//Determining if the first string contains the second string at any location
#a >> #b // true
#a->contains(#b) // true
 
//Determining if the first string ends with the second string
#a->endswith(#b) // false</lang>
 
=={{header|Liberty BASIC}}==
Line 2,278 ⟶ 2,274:
show ends.with? "house "doghouse ; true
show substring? "gho "doghouse ; true (built-in)</lang>
 
=={{header|Lua}}==
<lang lua>s1 = "string"
Line 2,292 ⟶ 2,289:
print( "s1 ends with s2: ", select( 2, string.find( s1, s2 ) ) == string.len( s1 ) )
print( "s1 ends with s3: ", select( 2, string.find( s1, s3 ) ) == string.len( s1 ) )</lang>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 2,319 ⟶ 2,317:
StringMatch
</lang>
 
=={{header|Maple}}==
These facilities are all to be found in the StringTools package in Maple.
Line 2,366 ⟶ 2,365:
True
{{1,3},{8,10},{15,17},{18,20}}</pre>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 2,489 ⟶ 2,487:
 
----
 
=={{header|NewLISP}}==
<lang NewLISP>(setq str "abcdefbcghi")
Line 2,524 ⟶ 2,523:
Ends with: brown fox
" brown " is located at position: 9</pre>
 
=={{header|Objective-C}}==
<lang objc>[@"abcd" hasPrefix:@"ab"] //returns true
[@"abcd" hasSuffix:@"zn"] //returns false
int loc = [@"abab" rangeOfString:@"bb"].location //returns -1
loc = [@"abab" rangeOfString:@"ab"].location //returns 0
loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2</lang>
 
=={{header|Objeck}}==
Line 2,548 ⟶ 2,540:
}
</lang>
 
=={{header|Objective-C}}==
<lang objc>[@"abcd" hasPrefix:@"ab"] //returns true
[@"abcd" hasSuffix:@"zn"] //returns false
int loc = [@"abab" rangeOfString:@"bb"].location //returns -1
loc = [@"abab" rangeOfString:@"ab"].location //returns 0
loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2</lang>
 
=={{header|OCaml}}==
Line 2,751 ⟶ 2,750:
<lang perl>print $-[0], "\n" while $str1 =~ /\Q$str2\E/g; # using a regex</lang>
<lang perl>my $i = -1; print $i, "\n" while ($i = index $str1, $str2, $i + 1) != -1; # using index</lang>
 
=={{header|Perl 6}}==
 
Using string methods:
 
<lang perl6>$haystack.starts-with($needle) # True if $haystack starts with $needle
$haystack.contains($needle) # True if $haystack contains $needle
$haystack.ends-with($needle) # True if $haystack ends with $needle</lang>
 
Using regexes:
 
<lang perl6>so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
so $haystack ~~ / $needle / # True if $haystack contains $needle
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle</lang>
 
Using <code>substr</code>:
 
<lang perl6>substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle</lang>
 
Bonus task:
 
<lang perl6>$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
$haystack.indices($needle :overlap); # Also find any overlapping instances of $needle in $haystack</lang>
 
=={{header|Phix}}==
Line 2,984 ⟶ 2,959:
end;
</lang>
 
=={{header|PowerShell}}==
<lang Powershell>
"spicywiener".StartsWith("spicy")
"spicywiener".Contains("icy")
"spicywiener".EndsWith("wiener")
"spicywiener".IndexOf("icy")
[regex]::Matches("spicywiener", "i").count
</lang>
{{out}}
<pre>
True
True
True
2
2
</pre>
 
=={{header|PureBasic}}==
Line 3,069 ⟶ 3,061:
0
1</pre>
 
=={{header|PowerShell}}==
<lang Powershell>
"spicywiener".StartsWith("spicy")
"spicywiener".Contains("icy")
"spicywiener".EndsWith("wiener")
"spicywiener".IndexOf("icy")
[regex]::Matches("spicywiener", "i").count
</lang>
{{out}}
<pre>
True
True
True
2
2
</pre>
 
=={{header|Python}}==
Line 3,113 ⟶ 3,088:
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Using string methods:
 
<lang perl6>$haystack.starts-with($needle) # True if $haystack starts with $needle
$haystack.contains($needle) # True if $haystack contains $needle
$haystack.ends-with($needle) # True if $haystack ends with $needle</lang>
 
Using regexes:
 
<lang perl6>so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
so $haystack ~~ / $needle / # True if $haystack contains $needle
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle</lang>
 
Using <code>substr</code>:
 
<lang perl6>substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle</lang>
 
Bonus task:
 
<lang perl6>$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
$haystack.indices($needle :overlap); # Also find any overlapping instances of $needle in $haystack</lang>
 
=={{header|Retro}}==
10,327

edits