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;
/*//from   w ww .ja  va 2 s .  c  om
 * 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;
    }

    /**
     * Replace all occurrences of the search string with the replace string
     * in the source string. If any of the strings is null or the search string
     * is zero length, the source string is returned.
     * @param source the source string whose contents will be altered
     * @param search the string to search for in source
     * @param replace the string to substitute for search if present
     * @return source string with *all* occurrences of the search string
     * replaced with the replace string
     */
    public static String replaceAll(String source, String search, String replace) {
        if (source != null && search != null && search.length() > 0 && replace != null) {
            int start = source.indexOf(search);
            if (start > -1) {
                StringBuffer newString = new StringBuffer(source);
                replaceAll(newString, search, replace);
                return newString.toString();
            }
        }
        return source;
    }

    /**
     * @param source the source string whose contents will be altered
     * @param search the string to search for in source
     * @param replace the string to substitute for search if present
     */
    public static void replaceAll(StringBuffer source, String search, String replace) {
        if (source != null && search != null && search.length() > 0 && replace != null) {
            int start = source.toString().indexOf(search);
            while (start > -1) {
                int end = start + search.length();
                source.replace(start, end, replace);
                start = source.toString().indexOf(search, start + replace.length());
            }
        }
    }

    /**
     * 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)
  9. replaceWhitespaces(String inString, String with)