Java String Sanitize sanitize(String string, String spaceReplace)

Here you can find the source of sanitize(String string, String spaceReplace)

Description

Sanitizes the provided message, removing any non-alphanumeric characters and swapping spaces with the specified string.

License

Open Source License

Parameter

Parameter Description
string The message to be sanitized.
spaceReplace The string to be substituted for spaces.

Return

The sanitized string.

Declaration

public static String sanitize(String string, String spaceReplace) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w w  w .  j a va 2  s.  c om
     * Sanitizes the provided message, removing any non-alphanumeric characters and swapping spaces with the specified
     * string.
     * <p/>
     * Examples: sanitize("Hello! :) How are you?", '-') --> "Hello--How-are-you" sanitize("I am great, thank you!",
     * '*') --> "I*am*great*thank*you"
     *
     * @param string       The message to be sanitized.
     * @param spaceReplace The string to be substituted for spaces.
     * @return The sanitized string.
     */
    public static String sanitize(String string, String spaceReplace) {
        return string.replaceAll("[^\\dA-Za-z ]", "").replaceAll("\\s+", spaceReplace);
    }
}

Related

  1. sanitize(String s, boolean allowColorCodes)
  2. sanitize(String source)
  3. sanitize(String str)
  4. sanitize(String string)
  5. sanitize(String string, boolean allowWhitespace)
  6. sanitize(String text)
  7. sanitize(String text)
  8. sanitize(String value)
  9. sanitizeAppAlias(String appAlias)