|
| 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_\\s]", "") |
| 18 | + } |
| 19 | + |
| 20 | + def removeSpecialCharUsingWordRegex(text: String): String = { |
| 21 | + text.replaceAll("\\W", "") |
| 22 | + } |
| 23 | + |
| 24 | + def removeSpecialCharUsingAnotherWordRegex(text: String): String = { |
| 25 | + text.replaceAll("[^\\w\\s]", "") |
| 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_!") == "Hello Baeldung_" |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + it should "remove special characters using removeSpecialCharUsingWordRegex" in { |
| 77 | + assert( |
| 78 | + removeSomeSpecialCharUsingRegex( |
| 79 | + "Hello Baeldung_*()!" |
| 80 | + ) == "Hello Baeldung_" |
| 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 | + ) == "Hello Baeldung_" |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments