Jump to content

Multisplit: Difference between revisions

Added Kotlin
(Added Algol 68)
(Added Kotlin)
Line 858:
["a",["!="],"",["=="],"b",["="],"",["!="],"c"]
["aaa",["!="],"",["=="],"bbb",["="],"",["!="],"ccc"]
 
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
 
fun main(args: Array<String>) {
val input = "a!===b=!=c"
val delimiters = arrayOf("==", "!=", "=")
val output = input.split(*delimiters).toMutableList()
for (i in 0 until output.size) {
if (output[i].isEmpty()) output[i] = "empty string"
else output[i] = "\"" + output[i] + "\""
}
println("The splits are:")
println(output)
 
// now find positions of matched delimiters
val matches = mutableListOf<Pair<String, Int>>()
var index = 0
while (index < input.length) {
var matched = false
for (d in delimiters) {
if (input.drop(index).take(d.length) == d) {
matches.add(d to index)
index += d.length
matched = true
break
}
}
if (!matched) index++
}
println("\nThe delimiters matched and the indices at which they occur are:")
println(matches)
}</lang>
 
{{out}}
<pre>
The splits are:
["a", empty string, "b", empty string, "c"]
 
The delimiters matched and the indices at which they occur are:
[(!=, 1), (==, 3), (=, 6), (!=, 7)]
</pre>
 
=={{header|Lua}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.