Skip to content

Commit d093cc5

Browse files
Merge pull request #1227 from yadavan88/capitalise-words
Added sample code for sentence capialization
2 parents 009c9cc + de5e2e8 commit d093cc5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.scala.strings.capitalize
2+
3+
object CapitalizeWords {
4+
5+
def capitalizeWords(sentence: String): String = {
6+
sentence.split("\\s+").map(_.capitalize).mkString(" ")
7+
}
8+
9+
def capitalizeTitleCase(sentence: String): String = {
10+
val exclusions = Set("is", "in", "to", "a", "an", "the")
11+
sentence
12+
.split("\\s+")
13+
.zipWithIndex
14+
.map { (word, index) =>
15+
if (index != 0 && exclusions.contains(word.toLowerCase)) word
16+
else word.capitalize
17+
}
18+
.mkString(" ")
19+
}
20+
21+
def capitalizeWordsPreserveSpaces(sentence: String): String = {
22+
sentence.zipWithIndex.map { (char, index) =>
23+
if (index == 0) char.toUpper
24+
else if (sentence.charAt(index - 1).isSpaceChar) char.toUpper
25+
else char
26+
}.mkString
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.scala.strings.capitalize
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
import org.scalatest.prop.TableDrivenPropertyChecks
6+
7+
class CapitalizeWordsUnitTest
8+
extends AnyFlatSpec
9+
with Matchers
10+
with TableDrivenPropertyChecks {
11+
12+
val table = Table(
13+
("Input", "Expected"),
14+
("This is scala 3", "This Is Scala 3"),
15+
("hello world", "Hello World"),
16+
("baeldung articles", "Baeldung Articles"),
17+
(" ", ""),
18+
("1000", "1000")
19+
)
20+
it should "capitalize every words of a sentence" in {
21+
forAll(table) { (input, expected) =>
22+
CapitalizeWords.capitalizeWords(input) shouldBe expected
23+
}
24+
}
25+
26+
val tableWithExclusions = Table(
27+
("Input", "Expected"),
28+
("This is scala 3", "This is Scala 3"),
29+
("baeldung articles", "Baeldung Articles"),
30+
(" ", ""),
31+
(
32+
"the quick brown fox jumps over the lazy Dog",
33+
"The Quick Brown Fox Jumps Over the Lazy Dog"
34+
)
35+
)
36+
it should "capitalize every word of a sentence with exclusion" in {
37+
forAll(tableWithExclusions) { (input, expected) =>
38+
CapitalizeWords.capitalizeTitleCase(input) shouldBe expected
39+
}
40+
}
41+
42+
val tablePreservingSpace = Table(
43+
("Input", "Expected"),
44+
("This is scala 3", "This Is Scala 3"),
45+
("hello world", "Hello World"),
46+
("baeldung articles", "Baeldung Articles"),
47+
(" ", " "),
48+
("1000", "1000")
49+
)
50+
it should "capitalize every words of a sentence preserving the spaces" in {
51+
forAll(tablePreservingSpace) { (input, expected) =>
52+
CapitalizeWords.capitalizeWordsPreserveSpaces(input) shouldBe expected
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)