Removing White Spaces From Data in Spark

Updated On February 11, 2021 | By Mahesh Mogal

There are multiple methods provided by the spark to handle white spaces in data. The most basic way to remove white spaces is to use "regexp_replace". Unfortunately "regexp_replace" is not always easy to use. So we are going to learn some simple functions like trim, ltrim & rtrim to remove white spaces.

ltrim

We can use ltrim to remove white spaces from beginning of string.

rtrim

Just like ltrim, we can use rtrim to remove trailing white spaces from string.

trim

If we want to remove white spaces from both ends of string we can use the trim function.

We can easily check if this is working or not by using length function.

Removing White Spaces using Trim
Removing White Spaces using Trim

Using "regexp_replace" to remove white spaces

"regexp_replace" is powerful & multipurpose method. Let us see how we can use it to remove white spaces around string data in spark.

Obviously this regular expression removes all white space from a string. even space between words. By changing regular expression, you can use the above code for multiple use cases.

I hope you found this useful. See you in next blog 🙂

Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Spark Join Types With Examples

In this blog, we are going to learn different spark join types. We will also write code and validate data output for each join type to better understand them.

Read More
Integrate Spark with Jupyter Notebook and Visual Studio Code

In this blog, we are going to integrate spark with jupyter notebook and visual studio code to create easy-to-use development environment.

Read More
Reading Data From SQL Tables in Spark

In this blog, we are going to learn about reading data from SQL tables in Spark. We will create Spark data frames from tables and query results as well.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram