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 🙂

Removing White Spaces From Data in Spark

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

Where and Filter in Spark Dataframes

In this blog, we will learn how to filter rows from spark dataframe using Where and Filter functions.

Where and Filter in Spark Dataframes
Read More
Distinct Rows and Distinct Count from Spark Dataframe

Getting distinct values from columns or rows is one of most used operations. We will learn how to get distinct values as well as count of distinct values.

Distinct Rows and Distinct Count from Spark Dataframe
Read More
Sorting in Spark Dataframe

In this blog, we will learn how to sort rows in spark dataframe based on some column values.

Sorting in Spark Dataframe
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
Share via
Copy link
Powered by Social Snap