Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

624
Visualizações
Counting how many times specific character appears in string - Kotlin

How one may count how many times specific character appears in string in Kotlin?

From looking at https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/ there is nothing built-in and one needs to write loop every time (or may own extension function), but maybe I missed a better way to achieve this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Easy with filter {} function

val str = "123 123 333"

val countOfSymbol = str
        .filter { it == '3' } // 3 is your specific character
        .length

println(countOfSymbol) // output 5

Another approach

val countOfSymbol = str.count { it == '3'} // 3 is your specific character
println(countOfSymbol) // output 5

From the point of view of saving computer resources, the count decision(second approach) is more correct.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda