String matching: Difference between revisions
→{{header|Kotlin}}: updated kotlin to find ALL occurrences of the string
(add sed) |
(→{{header|Kotlin}}: updated kotlin to find ALL occurrences of the string) |
||
Line 2,640:
=={{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
// 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
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>
|