Explain Codes LogoExplain Codes Logo

How to remove duplicate white spaces in string using Java?

java
string-manipulation
regex
performance
Anton ShumikhinbyAnton Shumikhin·Aug 10, 2024
TLDR

To quickly reduce all multiple white spaces down to a single space in your Java string, use the replaceAll("\\s{2,}", " ") function:

// Oh look! A magical one-liner ✨ String compacted = text.replaceAll("\\s{2,}", " ");

The regular expression \\s{2,} stands for "two or more spaces", which are then replaced with a single space in the result string compacted.

Detailed exploration of white spaces

In Java, we deal with different types of white spaces: spaces, tabs, newlines. How you handle these can greatly impact the readability of your code and data.

String salon: maintaining the style but trimming the extras

The replaceAll method is a fantastic stylist at our String Salon. It maintains the original order of the words, just giving them a cleaner, more modern look with less white space.

Sometimes, you might want to give your string a complete makeover and trim leading and trailing spaces too. Nothing like a full trim and replaceAll treatment to get things looking sleek:

// Time for a trim! String trimmedAndCompacted = text.trim().replaceAll("\\s{2,}", " ");

Performance considerations: racing with Regex

When racing with large strings, performance is key. Even though the replaceAll method seems like a great shortcut, it may lead to pit stops in your performance by creating a Matcher internally each time it's used.

To avoid changing tires mid-race, consider creating a precompiled pattern. Pit crew, are you ready?

// Ready, set, precompile! Pattern pattern = Pattern.compile("\\s{2,}"); Matcher matcher = pattern.matcher(text); String optimizedCompacted = matcher.replaceAll(" ");

Advanced string manipulations

In the world of Java strings, more complex use-cases may require more sophisticated techniques. Let's check out some of those.

StringUtils to the rescue

If you're dealing with external libraries like Apache Commons Lang, the StringUtils.normalizeSpace() function is like a Swiss Army knife - it trims and removes duplicate whitespace all at once.

// Goodbye duplicates, hello cleaner string! String normalized = StringUtils.normalizeSpace(text);

The No-Regex diet

Too many regular expressions can bloat your code. If replaceAll feels a bit heavy for your liking, here's a lighter version using iterative replacements.

// This is like removing chocolate from a candy bar until you only have a one left. while (text.contains(" ")) { text = StringUtils.replace(text, " ", " "); }

Adapt and thrive in limited resources

Programming for resource-limited environments, like mobile apps, is like making a delicious sandwich with few ingredients - it's an art! Ensuring your code is performance-friendly is crucial here.

Mastering the nuances of white spaces

Java considers tabs, spaces, and newlines all as white spaces. Being thorough with this can help make your string manipulations more accurate.

Trimming the edges: a pre-launch checklist

A common gotcha is to forget to trim the start and end of the string before manipulating spaces. This could result in misplaced spaces, leading to a bumpy launch 🚀!

Edge cases: It's not a bug, it's a feature!

Depending on the specifics of your use-case, you may want to keep some types of white spaces, like code block indents. So, always double-check the requirements and expected behavior.