java - REGEX, remove whitespace and all other characters

Java - REGEX, remove whitespace and all other characters

If you want to remove whitespace and all other characters from a string using regular expressions in Java, you can use the replaceAll method along with an appropriate regex pattern. Here's an example:

public class RemoveWhitespaceAndCharacters { public static void main(String[] args) { String inputString = "Hello, 123 World!"; // Remove whitespace and all other characters String result = inputString.replaceAll("[^a-zA-Z0-9]", ""); System.out.println("Original String: " + inputString); System.out.println("Result: " + result); } } 

In this example:

  • The replaceAll("[^a-zA-Z0-9]", "") method removes all characters that are not letters (both lowercase and uppercase) or digits. The ^ inside the square brackets negates the character class, meaning it will match any character not in the specified set.

  • Adjust the regex pattern [a-zA-Z0-9] based on the specific characters you want to allow. If you want to include other characters or exclude specific characters, modify the regex accordingly.

  • The result will contain the original string without whitespace and other characters.

After running this program, the output will be:

Original String: Hello, 123 World! Result: Hello123World 

Modify the regex pattern based on your specific requirements for character inclusion or exclusion.

Examples

  1. Remove Whitespace from String in Java using REGEX:

    String stringWithWhitespace = "Hello World"; String result = stringWithWhitespace.replaceAll("\\s", ""); 
  2. Remove All Non-Alphanumeric Characters from String in Java using REGEX:

    String stringWithSpecialChars = "abc!@123#"; String result = stringWithSpecialChars.replaceAll("[^a-zA-Z0-9]", ""); 
  3. Remove Leading and Trailing Whitespace in Java:

    String stringWithWhitespace = " Trim Me "; String result = stringWithWhitespace.trim(); 
  4. Remove All Whitespaces from a String in Java:

    String stringWithWhitespaces = "Remove all spaces"; String result = stringWithWhitespaces.replaceAll("\\s", ""); 
  5. Remove Punctuation from String in Java using REGEX:

    String stringWithPunctuation = "Hello, World!"; String result = stringWithPunctuation.replaceAll("\\p{Punct}", ""); 
  6. Remove Digits from String in Java using REGEX:

    String stringWithDigits = "Java123"; String result = stringWithDigits.replaceAll("\\d", ""); 
  7. Remove Specific Characters from String in Java:

    String stringWithCharsToRemove = "Remove these characters: $%^"; String result = stringWithCharsToRemove.replaceAll("[$%^]", ""); 
  8. Remove Multiple Whitespaces with Single Whitespace in Java:

    String stringWithMultipleSpaces = "Reduce multiple spaces"; String result = stringWithMultipleSpaces.replaceAll("\\s+", " "); 
  9. Remove Line Breaks from String in Java:

    String stringWithLineBreaks = "Remove\nline\nbreaks"; String result = stringWithLineBreaks.replaceAll("\\r?\\n", ""); 
  10. Remove Whitespace at the Beginning or End of String in Java:

    String stringWithLeadingTrailingSpace = " Trim spaces "; String result = stringWithLeadingTrailingSpace.replaceAll("^\\s+|\\s+$", ""); 

More Tags

react-dom eigenvector statefulwidget subset predicate entity-framework-migrations seeding android-bottomnav pivot-table pre-commit-hook

More Programming Questions

More Electronics Circuits Calculators

More Housing Building Calculators

More Organic chemistry Calculators

More Various Measurements Units Calculators