String matching: Difference between revisions

m
(Added Odin variant)
 
(10 intermediate revisions by 10 users not shown)
Line 1,709:
(string-match "Antoinette" "net") → #t ;; contains
(string-index "net" "Antoinette") → 5 ;; substring location
</syntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight>
func starts s$ t$ .
if substr s$ 1 len t$ = t$
return 1
.
return 0
.
func ends s$ t$ .
if substr s$ (len s$ - len t$ + 1) len t$ = t$
return 1
.
return 0
.
func contains s$ t$ .
return if strpos s$ t$ > 0
.
print starts "hello world" "he"
print ends "hello world" "rld"
print contains "hello world" "wor"
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
Line 1,719 ⟶ 1,742:
var s := "abcd";
console.printLine(s," starts with ab: ",s.startingWith:("ab"));
console.printLine(s," starts with cd: ",s.startingWith:("cd"));
console.printLine(s," ends with ab: ",s.endingWith:("ab"));
console.printLine(s," ends with cd: ",s.endingWith:("cd"));
console.printLine(s," contains ab: ",s.containing:("ab"));
console.printLine(s," contains bc: ",s.containing:("bc"));
console.printLine(s," contains cd: ",s.containing:("cd"));
console.printLine(s," contains az: ",s.containing:("az"));
console.printLine(s," index of az: ",s.indexOf(0, "az"));
Line 2,304 ⟶ 2,327:
False
</pre>
 
=={{header|GDScript}}==
<syntaxhighlight lang="gdscript">
@tool
extends Node
 
@export var first_string: String
@export var second_string: String
 
@export var starts_with: bool:
get: return first_string.begins_with(second_string)
 
@export var contains: bool:
get: return first_string.contains(second_string)
 
@export var ends_with: bool:
get: return first_string.ends_with(second_string)
</syntaxhighlight>
 
=={{header|GML}}==
Line 2,486 ⟶ 2,527:
 
=={{header|Java}}==
For this task consider the following strings
<syntaxhighlight lang="java">
String string = "string matching";
String suffix = "ing";
</syntaxhighlight>
The most idiomatic way of determining if a string starts with another string is to use the ''String.startsWith'' method.
<syntaxhighlight lang="java">
string.startsWith(suffix)
</syntaxhighlight>
Another way is to use a combination of ''String.substring'' and ''String.equals''
<syntaxhighlight lang="java">
string.substring(0, suffix.length()).equals(suffix)
</syntaxhighlight>
To determine if a string contains at least one occurrence of another string, use the ''String.contains'' method.
<syntaxhighlight lang="java">
string.contains(suffix)
</syntaxhighlight>
A slightly more idiomatic approach would be to use the ''String.indexOf'' method, which will also return the index of the first character.
<syntaxhighlight lang="java">
string.indexOf(suffix) != -1
</syntaxhighlight>
The most idiomatic way of determining whether a string ends with another string is to use the ''String.endsWith'' method.
<syntaxhighlight lang="java">
string.endsWith(suffix);
</syntaxhighlight>
Similarly, a combination of ''String.substring'' and ''String.equals'' can be used.
<syntaxhighlight lang="java">
string.substring(string.length() - suffix.length()).equals(suffix)
</syntaxhighlight>
If you're looking to find the index of each occurrence, you can use the following.
<syntaxhighlight lang="java">
int indexOf;
int offset = 0;
while ((indexOf = string.indexOf(suffix, offset)) != -1) {
System.out.printf("'%s' @ %d to %d%n", suffix, indexOf, indexOf + suffix.length() - 1);
offset = indexOf + 1;
}
</syntaxhighlight>
<pre>
'ing' @ 3 to 5
'ing' @ 12 to 14
</pre>
<br />
Alternately
<syntaxhighlight lang="java">"abcd".startsWith("ab") //returns true
"abcd".endsWith("zn") //returns false
Line 2,492 ⟶ 2,577:
int loc = "abab".indexOf("bb") //returns -1
loc = "abab".indexOf("ab") //returns 0
loc = "abab".indexOf("ab",loc+1) //returns 2</syntaxhighlight>
</syntaxhighlight>
 
<syntaxhighlight lang="java">
// -----------------------------------------------------------//
public class JavaApplication6 {
 
public static void main(String[] args) {
String strOne = "complexity";
String strTwo = "udacity";
 
//
stringMatch(strOne, strTwo);
 
}
 
Line 2,531 ⟶ 2,612:
}
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 2,640 ⟶ 2,722:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// version 1.0.6
fun main() {
 
fun main(args: Array<String>) {
val s1 = "abracadabra"
val s2 = "abra"
println("$s1 begins with $s2 : ${s1.startsWith(s2)}")
println("$s1 ends with $s2 : ${s1.endsWith(s2)}")
val b = s2 in s1
if (b) {
print("$s1 contains $s2 : $b")
print("$s1 contains $s2 at these indices: ")
if (b) println(" at locations ${s1.indexOf(s2) + 1} and ${s1.lastIndexOf(s2) + 1}")
// can use indexOf to get first index or lastIndexOf to get last index
else println()
// to get ALL indices, use a for loop or Regex
println(
s2.toRegex(RegexOption.LITERAL).findAll(s1).joinToString { it.range.start.toString() }
)
}
else println("$s1 does not contain $2.")
}</syntaxhighlight>
 
{{out}}
<pre>
abracadabra begins with abra : true
abracadabra ends with abra : true
abracadabra contains abra at these indices: true at locations 1 and0, 87
</pre>
 
Line 3,595 ⟶ 3,682:
{1,16,33}
</pre>
 
=={{header|Phixmonti}}==
Simple solution
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/String_prepend
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
"the last thing the man said was the" "the" pstack
len var l >ps
1 l slice tps == if "Begins with keyword" ? endif
0 l - l slice tps == if "Ends with keyword" ? endif
tail ps> find dup if "Keyword appears first at " print 1 + print " position" ? else drop endif
drop
</syntaxhighlight>
{{out}}
<pre>
["the last thing the man said was the", "the"]
Begins with keyword
Ends with keyword
Keyword appears first at 16 position
 
=== Press any key to exit ===</pre>
More complex solution
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/String_prepend
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
"the last thing the man said was the" "the" pstack
( ) rot rot >ps
 
true while
tps find dup >ps
if swap tps 0 put swap 32 ps> set true else ps> endif
endwhile
 
len ps> len nip - 1 + >ps drop
 
len for get
dup 1 == if "Begins with keyword" ? drop else
dup tps == if "Ends with keyword" ? drop else
"Locate at position " print ?
endif endif
endfor
ps> drop
</syntaxhighlight>
{{out}}
<pre>
["the last thing the man said was the", "the"]
Begins with keyword
Locate at position 16
Ends with keyword
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
Line 4,265 ⟶ 4,407:
</syntaxhighlight>
 
=={{header|RPL}}==
===Determining if the first string starts with second string===
Returns 1 if the strings match accordingly, 0 otherwise.
"ABCDEF" "ABC"
≪ SWAP OVER SIZE 1 SWAP SUB == ≫ EVAL
===Determining if the first string contains the second string at any location===
Returns the position of the first character of the second string in the first string if the strings match accordingly, 0 otherwise.
"ABCDEF" "BCD"
POS
===Determining if the first string ends with the second string===
Returns 1 if the strings match accordingly, 0 otherwise.
"ABCDEF" "DEF"
≪ SWAP DUP2 SIZE SWAP SIZE - 1 + OVER SIZE SUB == ≫ EVAL
{{out}}
<pre>
3: 1
2: 2
1: 1
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">p 'abcd'.start_with?('ab') #returns true
Line 4,363 ⟶ 4,524:
loc = "abab".indexOf("ab") //returns 0
loc = "abab".indexOf("ab", loc+1) //returns 2</syntaxhighlight>
 
=={{header|sed}}==
The following programs handle the input lines pairwise: If the first string of a pair contains the second string, the former one is shown. (Which means that non-matching pairs are filtered out.)
 
1. Determining if the first string starts with the second string:
<syntaxhighlight lang="sed">N;/^\(.*\).*\n\1$/!d;s/\n.*//</syntaxhighlight>
2. Determining if the first string contains the second string at any location:
<syntaxhighlight lang="sed">N;/.*\(.*\).*\n\1$/!d;s/\n.*//</syntaxhighlight>
3. Determining if the first string ends with the second string:
<syntaxhighlight lang="sed">N;/\(.*\)\n\1$/!d;s/\n.*//</syntaxhighlight>
{{out}}
<pre>
$ printf '%s\n' abcd bcd wxyz wxy | sed -f match1.sed
wxyz
$ printf '%s\n' abcd be vwxyz wxy | sed -f match2.sed
vwxyz
$ printf '%s\n' abcd abc wxyz xyz | sed -f match3.sed
wxyz
</pre>
 
=={{header|Seed7}}==
Line 4,829 ⟶ 5,009:
works the same as in VBA, see [[String_matching#VBA]]
 
=={{header|V (Vlang)}}==
{{trans|Delphi}}
<syntaxhighlight lang="v (vlang)">fn main() {
str := 'abcd'
println(str.starts_with('ab')) // True
Line 4,850 ⟶ 5,030:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var s = "abracadabra"
var t = "abra"
var u = "ra"
1,995

edits