String matching: Difference between revisions
Add ed example
(Add ed example) |
|||
(11 intermediate revisions by 10 users not shown) | |||
Line 1,710:
(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|ed}}==
This script joins two strings (each on the line of its own) with vertical bar and then does regex matching against them.
<syntaxhighlight lang="sed">
H
s/^/|/
,j
t0
t0
1s/^(.*)(.*)\|\1$/String \1\2 starts with \1/
1s/^(.*)\|(.*)/String \1 does not start with \2/
2s/^(.*)(.*)(.*)\|\2$/String \1\2\3 contains \2/
2s/^(.*)\|(.*)/String \1 does not contain \2/
3s/^(.*)(.*)\|\2$/String \1\2 ends with \2/
3s/^(.*)\|(.*)/String \1 does not end with \2/
,p
Q
</syntaxhighlight>
{{out}}
<pre>$ cat string-matching.ed | ed -lEGs string-matching.input
String abab starts with ab
String abab contains ab
String abab ends with ab</pre>
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
Line 1,719 ⟶ 1,769:
var s := "abcd";
console.printLine(s," starts with ab: ",s.startingWith
console.printLine(s," starts with cd: ",s.startingWith
console.printLine(s," ends with ab: ",s.endingWith
console.printLine(s," ends with cd: ",s.endingWith
console.printLine(s," contains ab: ",s.containing
console.printLine(s," contains bc: ",s.containing
console.printLine(s," contains cd: ",s.containing
console.printLine(s," contains az: ",s.containing
console.printLine(s," index of az: ",s.indexOf(0, "az"));
Line 2,304 ⟶ 2,354:
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,554:
=={{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,604:
int loc = "abab".indexOf("bb") //returns -1
loc = "abab".indexOf("ab") //returns 0
loc = "abab".indexOf("ab",loc+1) //returns 2
</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,639:
}
}
</syntaxhighlight>
=={{header|JavaScript}}==
Line 2,640 ⟶ 2,749:
=={{header|Kotlin}}==
<syntaxhighlight lang="
fun main() {
val s1 = "abracadabra"
val s2 = "abra"
println("$s1 begins with $s2
println("$s1 ends with $s2
val b
if (b) {
print("$s1 contains $s2 at these indices: ")
// can use indexOf to get first index or lastIndexOf to get last index
// 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
abracadabra ends with abra
abracadabra contains abra at these
</pre>
Line 3,524 ⟶ 3,638:
1
};</syntaxhighlight>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
##
Println('abcd'.StartsWith('ab'));
Println('abcd'.EndsWith('cd'));
var abra := 'abracadabra';
Println('bra' in abra);
var ind := abra.IndexOf('bra');
var ind1 := abra.IndexOf('bra', ind + 1);
Println(ind,ind1);
abra.Matches('bra').Select(m -> m.Index).Println
</syntaxhighlight>
{{out}}
<pre>
True
True
True
1 8
1 8
</pre>
=={{header|Perl}}==
Line 4,320 ⟶ 4,456:
</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,418 ⟶ 4,573:
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,758 ⟶ 4,932:
0123
first line is a suffix of the second line</pre>
=={{header|Uiua}}==
{{works with|Uiua|0.11.1}}
Does <code>football</code> start with <code>foot</code>?
<syntaxhighlight lang="uiua">
⊢⦷"foot" "football" # 1
</syntaxhighlight>
Does <code>football</code> contain <code>otba</code>?
<syntaxhighlight lang="uiua">
/↥⌕"otba" "football" # 1
</syntaxhighlight>
Does <code>football</code> end with <code>ball</code>?
<syntaxhighlight lang="uiua">
⊢⇌⦷"ball" "football" # 1
</syntaxhighlight>
Where is <code>iss</code> in <code>Mississippi</code>?
<syntaxhighlight lang="uiua">
⊚⌕"iss" "Mississippi" # [1 4]
</syntaxhighlight>
=={{header|Vala}}==
Line 4,905 ⟶ 5,098:
=={{header|Wren}}==
<syntaxhighlight lang="
var t = "abra"
var u = "ra"
|