1. Introduction

Text processing is a fundamental aspect of many programming tasks. This often involves cleaning and manipulating text data to prepare it for further analysis. A common technique in data cleaning is string sanitization, which focuses on removing special characters from a string.

In this tutorial, we’ll look at how to remove the special characters from a Scala string.

2. Using Regular Expression

Regular expressions provide a powerful method for processing and manipulating text data based on a defined pattern. Let’s look at how we can apply the regular expression to remove special characters:

def removeAllSpecialCharUsingRegex(text: String): String = {
  text.replaceAll("[^a-zA-Z0-9]", "")

The above method removes all non-alphanumeric characters from a string. We can verify the implementation by writing a simple test:

assert(removeAllSpecialCharUsingRegex("Hello Baeldung_!") == "HelloBaeldung")

It removes all special characters, including spaces and underscores, leaving only alphanumeric characters.

Sometimes, preserving underscores alongside alphanumeric characters is beneficial while removing other special characters from the string. Let’s modify the above pattern to support this:

val text = "Hello Baeldung_!"
val sanitized = text.replaceAll("[^a-zA-Z0-9_]", "") 
assert(sanitized == "HelloBaeldung_")

We can achieve the same behavior using another simpler regular expression pattern:

val text = "Hello Baeldung_!" 
val sanitized = text.replaceAll("\\W", "") 
assert(sanitized == "HelloBaeldung_")

The pattern \\W matches any characters that are neither alphanumeric nor underscore. It’s important to note that the pattern \\W is the inverse of \\w.

3. Using filter()

Another way to remove special characters is by using the filter() method from the String class. Let’s look at the implementation:

val text = "Hello Baeldung_!"
val sanitized = text.filter(_.isLetterOrDigit)
assert(sanitized == "HelloBaeldung")

We can use the isLetterOrDigit() method to take only alphanumeric characters from the given string. We can also adjust the filter condition to include specific characters that we want to retain:

val text = "Hello Baeldung_!*&$"
val sanitized = text.filter(c => c.isLetterOrDigit || Set(' ', '_').contains(c))
assert(sanitized == "Hello Baeldung_")

Now, spaces and underscores are preserved along with alphanumeric characters.

4. Using collect()

Another way to solve this is using the collect() method. Let’s rewrite the filter() implementation using the collect() method:

val text = "Hello Baeldung_!*&$"
val sanitized = text.collect {
    case c if c.isLetterOrDigit || Set(' ', '_').contains(c) => c
assert(sanitized == "Hello Baeldung_")

The above method removes all special characters except spaces and underscores from the input string.

5. Conclusion

In this short article, we explored various methods to remove special characters from the given string. We covered approaches using regular expressions and the filter() and collect() methods. Each method provides different advantages, allowing us to choose the most suitable approach based on the specific requirements.

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

Notify of
Inline Feedbacks
View all comments