split

두문자어
두문자어 Scala에서
object Acronym:
  def abbreviate(phrase: String): String =
    raw"[\s_-]+".r
      .split(phrase)
      .map(_.head.toUpper)
      .mkString

This approach starts by defining a Regex pattern using a raw interpolator. The raw interpolator allows using the backslash escape character in a Regex pattern without having to escape the backslash with another backslash. The pattern looks for one or more whitespace, underscore or dash characters. The underscore is there for words with emphasis, such as _Not_.

The Regex calls its split() method, which splits the input phrase around all matches of the Regex. The resulting Array of words is chained to the map() method, which calls toUpper on the head of each word and passes the uppercased first character of each word to the mkString method, which returns all of those characters from the abbreviate() method as a String.

2026년 11월 16일 · 유용했나요?

Scala의 두문자어에 대한 다른 접근법

우리 커뮤니티가 이 연습 문제를 해결한 다른 방법