Java Regex String Replace Whitespace replaceWhitespace(final String value, final boolean stripExtras)

Here you can find the source of replaceWhitespace(final String value, final boolean stripExtras)

Description

Replaces all "whitespace" characters from the specified string with space characters, where whitespace includes \r\t\n and other characters

License

Open Source License

Parameter

Parameter Description
value the string to work with
stripExtras if true, replace multiple whitespace characters with a single character.

Declaration

public static String replaceWhitespace(final String value, final boolean stripExtras) 

Method Source Code

//package com.java2s;
/*//  w ww  .j  a va  2 s . c  o  m
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

public class Main {
    /**
     * Replaces all "whitespace" characters from the specified string with space characters, where whitespace includes \r\t\n and
     * other characters
     * 
     * @param value the string to work with
     * @param stripExtras if true, replace multiple whitespace characters with a single character.
     * @see java.util.regex.Pattern
     */
    public static String replaceWhitespace(final String value, final boolean stripExtras) {
        return replaceWhitespace(value, " ", stripExtras); //$NON-NLS-1$
    }

    /**
     * Replaces all "whitespace" characters from the specified string with space characters, where whitespace includes \r\t\n and
     * other characters
     * 
     * @param value the string to work with
     * @param replaceWith the character to replace with
     * @param stripExtras if true, replace multiple whitespace characters with a single character.
     * @see java.util.regex.Pattern
     */
    public static String replaceWhitespace(final String value, final String replaceWith,
            final boolean stripExtras) {
        String rv = value.replaceAll("\\s+", replaceWith); //$NON-NLS-1$

        if (stripExtras)
            rv = removeExtraWhitespace(rv);

        return rv;
    }

    /**
     * Replaces multiple sequential "whitespace" characters from the specified string with a single space character, where
     * whitespace includes \r\t\n and other characters
     * 
     * @param value the string to work with
     * @see java.util.regex.Pattern
     */
    public static String removeExtraWhitespace(final String value) {
        return value.replaceAll("\\s\\s+", " "); //$NON-NLS-1$//$NON-NLS-2$
    }
}

Related

  1. replaceBlank(String str)
  2. replaceBlank(String str)
  3. replaceBlank(String str)
  4. replaceBlank(String str)
  5. replaceWhitespace(final String value, final boolean stripExtras)
  6. replaceWhitespace(String searchTerm)
  7. replaceWhiteSpaces(final String value, final String replaceBy)
  8. replaceWhitespaces(String inString, String with)