object StringManipulationExamples {
def main(args: Array[String]): Unit = {
// 问题1
val floatString = "3.14"
val floatValue = floatString.toFloat
val backToString = floatValue.toString
println(backToString)
// 问题2
val originalString = "Hello, World! Scala is amazing."
val subString = originalString.substring(14)
val newString = subString + " Programming in Scala is fun."
println(newString)
// 问题3
val infoString = "John,20,85"
val parts = infoString.split(",")
val age = parts(1).toInt
val score = parts(2).toInt
println(s"Age: $age, Score: $score")
// 问题4
val inputString = "hello world hello scala world scala"
val words = inputString.split(" ")
val wordCountMap = scala.collection.mutable.Map[String, Int]()
for (word <- words) {
if (wordCountMap.contains(word)) {
wordCountMap(word) += 1
} else {
wordCountMap(word) = 1
}
}
for ((word, count) <- wordCountMap) {
println(s"$word: $count")
}
}
}
标签:练习题,String,val,scala,Scala,println,parts,字符串
From: https://blog.csdn.net/2401_83599586/article/details/143850184