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 111 views
1 answer 123 views
1 answer 90 views
1 answer 107 views
1 answer 79 views
...