Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wheeliechamp/77d465906364110d6e04284c7fbe8b32 to your computer and use it in GitHub Desktop.
Save wheeliechamp/77d465906364110d6e04284c7fbe8b32 to your computer and use it in GitHub Desktop.
kotlin 指定文字列の出現数をカウント
// 指定文字列の出現数をカウント
fun main(args: Array<String>) {
var hashmap = mutableMapOf<String, String>()
hashmap["aaa"] = "WWDDDWWDDDDLWDDDD"
hashmap["bbb"] = "WWDDDWWDDDDLWDDDD"
var dcount = mutableMapOf<String, Int>()
for((key, value) in hashmap) {
println(key)
println(value)
var str: String = value
var findstrlist = listOf("DDDDD", "DDDD", "DDD")
for(findstr: String in findstrlist) {
var len1 = str.length
str = str.replace(findstr, "")
var len2 = str.length
var count = (len1 - len2) / findstr.length
println("$findstr, $count")
if (dcount[findstr] == null) {
dcount[findstr] = count
} else {
dcount[findstr] = dcount[findstr]!! + count
}
}
}
println(dcount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment