Skip to content

Commit 25a91a6

Browse files
Merge pull request #1295 from yadavan88/remove-special-chars
Added sample code for removing special characters from a string
2 parents 15274a3 + 9343bb8 commit 25a91a6

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.scala.strings.removespecialchars
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
import org.scalatest.prop.TableDrivenPropertyChecks
6+
7+
class RemoveSpecialCharactersUnitTest
8+
extends AnyFlatSpec
9+
with Matchers
10+
with TableDrivenPropertyChecks {
11+
12+
def removeAllSpecialCharUsingRegex(text: String): String = {
13+
text.replaceAll("[^a-zA-Z0-9]", "")
14+
}
15+
16+
def removeSomeSpecialCharUsingRegex(text: String): String = {
17+
text.replaceAll("[^a-zA-Z0-9_]", "")
18+
}
19+
20+
def removeSpecialCharUsingWordRegex(text: String): String = {
21+
text.replaceAll("\\W", "")
22+
}
23+
24+
def removeSpecialCharUsingAnotherWordRegex(text: String): String = {
25+
text.replaceAll("[^\\w]", "")
26+
}
27+
28+
def removeAllSpecialCharsUsingFilter(text: String): String = {
29+
text.filter(_.isLetterOrDigit)
30+
}
31+
32+
def removeSpecialCharsUsingCollect(text: String): String = {
33+
text.collect {
34+
case c if c.isLetterOrDigit || Set(' ', '_').contains(c) => c
35+
}
36+
}
37+
38+
private val removeAllSpecialTable = Table(
39+
("text", "expected"),
40+
("Special_Chars**Text $#^{} Here 2", "SpecialCharsTextHere2"),
41+
(" ", ""),
42+
("!@#$.... ", ""),
43+
("baeldung", "baeldung")
44+
)
45+
46+
it should "remove all the special characters using regex" in {
47+
forAll(removeAllSpecialTable) { (text, expected) =>
48+
removeAllSpecialCharUsingRegex(text) shouldBe expected
49+
}
50+
}
51+
52+
it should "remove all the special characters using filter" in {
53+
forAll(removeAllSpecialTable) { (text, expected) =>
54+
removeAllSpecialCharsUsingFilter(text) shouldBe expected
55+
}
56+
}
57+
58+
it should "remove all special characters using removeAllSpecialCharUsingRegex" in {
59+
assert(
60+
removeAllSpecialCharUsingRegex("Hello Baeldung_!") == "HelloBaeldung"
61+
)
62+
}
63+
64+
it should "remove all special characters using removeAllSpecialCharsUsingFilter" in {
65+
assert(
66+
removeAllSpecialCharsUsingFilter("Hello Baeldung_!") == "HelloBaeldung"
67+
)
68+
}
69+
70+
it should "remove some special characters using removeSomeSpecialCharUsingRegex" in {
71+
assert(
72+
removeSomeSpecialCharUsingRegex("Hello Baeldung_!") == "HelloBaeldung_"
73+
)
74+
}
75+
76+
it should "remove special characters using removeSpecialCharUsingWordRegex" in {
77+
assert(
78+
removeSpecialCharUsingWordRegex(
79+
"Hello Baeldung_*()!"
80+
) == "HelloBaeldung_"
81+
)
82+
}
83+
84+
it should "remove special characters using removeSpecialCharsUsingCollect" in {
85+
assert(
86+
removeSpecialCharsUsingCollect("Hello Baeldung_*()!") == "Hello Baeldung_"
87+
)
88+
}
89+
90+
it should "remove special characters using removeSpecialCharUsingAnotherWordRegex" in {
91+
assert(
92+
removeSpecialCharUsingAnotherWordRegex(
93+
"Hello Baeldung_*()!"
94+
) == "HelloBaeldung_"
95+
)
96+
}
97+
98+
it should "filter only required character from string" in {
99+
val text = "Hello Baeldung_!*&$"
100+
val sanitized =
101+
text.filter(c => c.isLetterOrDigit || Set(' ', '_').contains(c))
102+
assert(sanitized == "Hello Baeldung_")
103+
}
104+
105+
}

0 commit comments

Comments
 (0)