Java String Sanitize sanitizeForUri(String uri, String replace)

Here you can find the source of sanitizeForUri(String uri, String replace)

Description

Sanitizes a URI to conform with the URI standards RFC3986 http://www.ietf.org/rfc/rfc3986.txt.

License

LGPL

Parameter

Parameter Description
uri The un-sanitized URI
replace The character to replace the disallowed characters with

Return

The sanitized input

Declaration

public static final String sanitizeForUri(String uri, String replace) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*from  w w w .j a  v a 2s  .c o m*/
     * Sanitizes a URI to conform with the URI standards RFC3986 http://www.ietf.org/rfc/rfc3986.txt.
     * <p>
     * Replaces all the disallowed characters for a correctly formed URI.
     * @param uri The un-sanitized URI
     * @param replace The character to replace the disallowed characters with
     * @return The sanitized input
     */
    public static final String sanitizeForUri(String uri, String replace) {
        /*
         * Explanation:
         * [a-zA-Z0-9\\._-] matches a letter from a-z lower or uppercase, numbers, dots, underscores and hyphen
         * [^a-zA-Z0-9\\._-] is the inverse. i.e. all characters which do not match the first expression
         * [^a-zA-Z0-9\\._-]+ is a sequence of characters which do not match the first expression
         * So every sequence of characters which does not consist of characters from a-z, 0-9 or . _ - will be replaced.
         */
        uri = uri.replaceAll("[^a-zA-Z0-9\\._-]+", replace);
        return uri;
    }
}

Related

  1. sanitizeForJson(String data)
  2. sanitizeForLogMessage(String unsanitizedString)
  3. sanitizeForSearch(String str)
  4. sanitizeForSemgrexName(String text)
  5. sanitizeForTableName(String input)
  6. sanitizeFullPrefixKey(String propKey)
  7. sanitizeGoogleId(String rawGoogleId)
  8. sanitizeHeader(String header)
  9. sanitizeID(String name)