1. Introduction

The Scala standard library provides many powerful string manipulation operations. In this brief tutorial, we’ll explore how to capitalize each word in a sentence using the standard library.

2. A Straightforward Solution

In this section, let’s examine a straightforward implementation of the capitalization of each word in a sentence:

def capitalizeWords(sentence: String): String = {
  sentence.split("\\s+").map(_.capitalize).mkString(" ")
}

In this approach, we split the sentence into different words using the split() method. The regular expression above eliminates multiple spaces during the splitting process.

The capitalize() method is then applied to each word to capitalize the first letter of the provided string.

We then join the words into a single text using mkString().

However, we should note that the chosen regular expression removes any extra spaces from the output.

2.1. Testing the Implementation

Now, we’ll create a unit test to validate the correctness of our implementation. We’ll utilize ScalaTest to incorporate unit tests:

val table = Table(
  ("Input", "Expected"),
  ("This is scala 3", "This Is Scala 3"),
  ("hello world", "Hello World"),
  ("baeldung   articles", "Baeldung Articles"),
  ("   ", ""),
  ("1000", "1000")
)
it should "capitalize every word of a sentence" in {
  forAll(table) { (input, expected) =>
    CapitalizeWords.capitalizeWords(input) shouldBe expected
  }
}

In this instance, we employed parameterized tests to comprehensively cover all scenarios, ensuring the correctness of our implementation.

3. Empty Space-Based Solution

Another approach to perform this task involves comparing each character with its preceding character to determine if there’s any space present. Let’s look at the implementation:

def capitalizeWordsPreserveSpaces(sentence: String): String = {
  sentence.zipWithIndex.map { (char, index) =>
    if(index == 0) char.toUpper
    else if(sentence.charAt(index-1).isSpaceChar) char.toUpper
    else char
  }.mkString
}

In this method, we inspect each character to determine if the preceding character is a space. If it’s, we convert the current character to uppercase. This approach guarantees that space characters remain unchanged and retain their original positions within the sentence.

3.1. Testing the Implementation

Let’s write the unit test to check if the solution works as expected:

val tablePreservingSpace = Table(
  ("Input", "Expected"),
  ("This is scala 3", "This Is Scala 3"),
  ("hello world", "Hello World"),
  ("baeldung   articles", "Baeldung   Articles"),
  ("   ", "   "),
  ("1000", "1000")
)
it should "capitalize every word of a sentence preserving the spaces" in {
  forAll(tablePreservingSpace) { (input, expected) =>
    CapitalizeWords.capitalizeWordsPreserveSpaces(input) shouldBe expected
  }
}

We can see that the space characters in the original sentences are preserved.

4. Title Case Based Solution

The title case capitalization is preferred in certain scenarios, such as publishing. The title case involves capitalizing only major words while retaining others in lowercase.

In this section, we’ll adjust the previous implementation to accommodate this requirement:

def capitalizeTitleCase(sentence: String): String = {
  val exclusions = Set("is", "in", "to", "a", "an", "the")
  sentence
    .split("\\s+")
    .zipWithIndex
    .map { (word, index) =>
      if (index != 0 && exclusions.contains(word.toLowerCase)) word else word.capitalize
    }
    .mkString(" ")
}

In this case, we defined a set of words that should remain uncapitalized. We proceed by verifying whether each word falls within this exclusion list. If it doesn’t, we capitalize it. Notably, we ensure that the first word is always capitalized, even if it is among the excluded words. To facilitate this process, we employed the zipWithIndex() method to access the word indexes.

It’s important to note that capitalization rules, especially for title cases, are not universally standardized and may vary depending on the specific context in which they are applied. In this implementation, we can tailor the exclusion list based on the specific requirements.

4.1. Testing the Implementation

We can create a corresponding unit test to verify the functionality of the implementation:

val tableWithExclusions = Table(
  ("Input", "Expected"),
  ("This is scala 3", "This is Scala 3"),
  ("baeldung   articles", "Baeldung Articles"),
  ("   ", ""),
  ("the quick brown fox jumps over the lazy Dog", "The Quick Brown Fox Jumps Over the Lazy Dog")
)
it should "capitalize every word of a sentence with exclusion" in {
  forAll(tableWithExclusions) { (input, expected) =>
    CapitalizeWords.capitalizeTitleCase(input) shouldBe expected
  }
}

We’ve once more utilized parameterized tests to cover a range of scenarios.

5. Conclusion

This article explored how to capitalize each word in a sentence using Scala, leveraging the powerful string manipulation operations available in the standard library. Furthermore, we extended the implementation to support title case capitalization and demonstrated it accompanied by thorough testing using ScalaTest.

As always, the sample code used in this tutorial is available over on GitHub.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments