How to count the white spaces in a string in Scala

1 Answer

0 votes
import scala.util.Try

def countWhitespaceCharacters(str: String): Int = {
  str.count(_.isWhitespace)
}

val str = "Scala \n  Programming \r Language \t "

println(s"Total white spaces: ${countWhitespaceCharacters(str)}")



   
/*
run:
   
Total white spaces: 10
   
*/

 

 



answered Oct 20, 2024 by avibootz

Related questions

1 answer 119 views
1 answer 127 views
1 answer 95 views
1 answer 114 views
1 answer 86 views
...