Created
April 27, 2021 14:43
-
-
Save wheeliechamp/77d465906364110d6e04284c7fbe8b32 to your computer and use it in GitHub Desktop.
kotlin 指定文字列の出現数をカウント
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 指定文字列の出現数をカウント | |
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