Avatar ·

Removing spaces from a string in Java: how to remove all spaces and tabs?

Hello everyone! I'm a beginner in Java and writing a small application for processing user input. I need to remove all spaces, tabs, and other whitespace characters from a string to get a "clean" sequence of characters. The problem is that I can't find the right way. I tried the `trim()` method, but it only removes spaces at the beginning and end of the string. For example: ```java String input = " Hello, world! "; String trimmed = input.trim(); System.out.println("'" + trimmed + "'"); // Output: 'Hello, world!' — spaces inside remain! ``` I need the string to become `"Hello,world!"`. I tried using `replaceAll(" ", "")`, but this only removes regular spaces (code 32), and what if there are tabs (`\t`) or non-breaking spaces inside? They remain.