String matching: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: multiple matches with 'indices')
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 62: Line 62:
CD
CD
</pre>
</pre>



=={{header|Ada}}==
=={{header|Ada}}==
Line 268: Line 267:
{{Out}}
{{Out}}
<pre>{8, 44, 83, 110}</pre>
<pre>{8, 44, 83, 110}</pre>

=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
Line 603: Line 603:
}
}
</lang>
</lang>



=={{header|BASIC}}==
=={{header|BASIC}}==
Line 744: Line 743:


Press any key to continue . . .</pre>
Press any key to continue . . .</pre>



=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 908: Line 906:
matching `something random' with `Rand':
matching `something random' with `Rand':
end match</pre>
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#}}==
=={{header|C sharp|C#}}==
Line 942: Line 924:
}
}
</lang>
</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}}==
=={{header|Clojure}}==
Line 1,181: Line 1,179:
2
2
1</pre>
1</pre>

=={{header|DCL}}==
=={{header|DCL}}==
<lang DCL>$ first_string = p1
<lang DCL>$ first_string = p1
Line 1,407: Line 1,406:
Acc.
Acc.
</lang>
</lang>

=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang euphoria>sequence first, second
<lang euphoria>sequence first, second
Line 2,061: Line 2,061:
In fact, it happens 2 times within 'tacoloco', at indexes 2, 6.
In fact, it happens 2 times within 'tacoloco', at indexes 2, 6.
3: Does 'tacoloco' end with 'co'? Yes.</pre>
3: Does 'tacoloco' end with 'co'? Yes.</pre>



=={{header|jq}}==
=={{header|jq}}==
Line 2,100: Line 2,099:
1
1
5</lang>
5</lang>



=={{header|Julia}}==
=={{header|Julia}}==
Line 2,134: Line 2,132:
_ss["abcdabceabc";"abc"] / location of matches
_ss["abcdabceabc";"abc"] / location of matches
0 4 8</lang>
0 4 8</lang>



=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 2,162: Line 2,159:
[[File:LabVIEW_Character_matching_2.png]]<br/>
[[File:LabVIEW_Character_matching_2.png]]<br/>
[[File:LabVIEW_Character_matching_3.png]]
[[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}}==
=={{header|Lang5}}==
Line 2,202: Line 2,181:
"rosettacode" "edoc" end-with . # 0
"rosettacode" "edoc" end-with . # 0
"rosettacode" "code" contains . # 7</lang>
"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}}==
=={{header|Liberty BASIC}}==
Line 2,278: Line 2,274:
show ends.with? "house "doghouse ; true
show ends.with? "house "doghouse ; true
show substring? "gho "doghouse ; true (built-in)</lang>
show substring? "gho "doghouse ; true (built-in)</lang>

=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>s1 = "string"
<lang lua>s1 = "string"
Line 2,292: Line 2,289:
print( "s1 ends with s2: ", select( 2, string.find( s1, s2 ) ) == string.len( s1 ) )
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>
print( "s1 ends with s3: ", select( 2, string.find( s1, s3 ) ) == string.len( s1 ) )</lang>

=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
<lang M2000 Interpreter>
Line 2,319: Line 2,317:
StringMatch
StringMatch
</lang>
</lang>

=={{header|Maple}}==
=={{header|Maple}}==
These facilities are all to be found in the StringTools package in Maple.
These facilities are all to be found in the StringTools package in Maple.
Line 2,366: Line 2,365:
True
True
{{1,3},{8,10},{15,17},{18,20}}</pre>
{{1,3},{8,10},{15,17},{18,20}}</pre>



=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
Line 2,489: Line 2,487:


----
----

=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>(setq str "abcdefbcghi")
<lang NewLISP>(setq str "abcdefbcghi")
Line 2,524: Line 2,523:
Ends with: brown fox
Ends with: brown fox
" brown " is located at position: 9</pre>
" 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}}==
=={{header|Objeck}}==
Line 2,548: Line 2,540:
}
}
</lang>
</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}}==
=={{header|OCaml}}==
Line 2,751: Line 2,750:
<lang perl>print $-[0], "\n" while $str1 =~ /\Q$str2\E/g; # using a regex</lang>
<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>
<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}}==
=={{header|Phix}}==
Line 2,984: Line 2,959:
end;
end;
</lang>
</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}}==
=={{header|PureBasic}}==
Line 3,069: Line 3,061:
0
0
1</pre>
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}}==
=={{header|Python}}==
Line 3,113: Line 3,088:
</pre>
</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}}==
=={{header|Retro}}==