DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • File Upload Security and Malware Protection
  • Getting Started With the YugabyteDB Managed REST API

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • File Upload Security and Malware Protection
  • Getting Started With the YugabyteDB Managed REST API
  1. DZone
  2. Coding
  3. Java
  4. Clojure: Stripping all the Whitespace

Clojure: Stripping all the Whitespace

Mark Needham user avatar by
Mark Needham
·
Oct. 03, 13 · Interview
Like (0)
Save
Tweet
Share
3.34K Views

Join the DZone community and get the full member experience.

Join For Free

When putting together data sets to play around with, one of the more boring tasks is stripping out characters that you’re not interested in and more often than not those characters are white spaces.

Since I’ve been building data sets using Clojure I wanted to write a function that would do this for me.

I started out with the following string:

(def word " with a  little bit of space we can make it through the night  ")

which I wanted to format in such a way that there would be a maximum of one space between each word.

I start out by using the trim function but that only removes white space from the beginning and end of a string:

> (clojure.string/trim word)
"with a  little bit of space we can make it through the night"

I wanted to get rid of the space in between ‘a’ and ‘little’ as well so I wrote the following code to split on a space and filter out any excess spaces that still remained before joining the words back together:

> (clojure.string/join " " 
                       (filter #(not (clojure.string/blank? %)) 
                               (clojure.string/split word #" ")))
"with a little bit of space we can make it through the night"

I wanted to try and make it a bit easier to read by using the thread last (->>) macro but that didn’t work as well as I’d hoped because clojure.string/split doesn’t take the string in as its last parameter:

>  (->> (clojure.string/split word #" ") 
   (filter #(not (clojure.string/blank? %))) 
   (clojure.string/join " "))
"with a little bit of space we can make it through the night"

I worked around it by creating a specific function for splitting on a space:

(defn split-on-space [word] 
  (clojure.string/split word #"\s"))

which means we can now chain everything together nicely:

>  (->> word 
        split-on-space 
        (filter #(not (clojure.string/blank? %))) 
        (clojure.string/join " "))
"with a little bit of space we can make it through the night"

I couldn’t find a cleaner way to do this but I’m sure there is one and my googling just isn’t up to scratch so do let me know in the comments!

Clojure

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • File Upload Security and Malware Protection
  • Getting Started With the YugabyteDB Managed REST API

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: