String matching: Difference between revisions

Add Uiua
(GDScript)
(Add Uiua)
(6 intermediate revisions by 5 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,504 ⟶ 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,510 ⟶ 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,549 ⟶ 2,612:
}
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 3,547 ⟶ 3,611:
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,819 ⟶ 4,905:
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,966 ⟶ 5,071:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var s = "abracadabra"
var t = "abra"
var u = "ra"
1,827

edits