iamdeep
iamdeep

Reputation: 51

Finding length of string in Scala

I am a newbie to scala I have a list of strings -
List[String] (“alpha”, “gamma”, “omega”, “zeta”, “beta”)

I want to count all the strings with length = 4

i.e I want to get output = 2.

Upvotes: 3

Views: 12480

Answers (2)

Learner
Learner

Reputation: 1190

You can do like this:

val data = List("alpha", "gamma", "omega", "zeta", "beta")

data.filter(x => x.length == 4).size

res8: Int = 2

Upvotes: 8

Ramesh Maharjan
Ramesh Maharjan

Reputation: 41987

You can just use count function as

val list =  List[String] ("alpha", "gamma", "omega", "zeta", "beta")

println(list.count(x => x.length == 4))
//2 is printed

I hope the answer is helpful

Upvotes: 5

Related Questions