Java String Sanitize sanitizeDir(String name)

Here you can find the source of sanitizeDir(String name)

Description

Takes a directory name and removes any illegal characters and shortens it to 64 characters if needed.

License

Open Source License

Parameter

Parameter Description
name The name of the directory to clean.

Return

The sanitized directory name.

Declaration

public static String sanitizeDir(String name) 

Method Source Code

//package com.java2s;
/* Profile Manager//from   w w w  .  j ava  2s.co m
 * Copyright (C) 2012 Curtis Oakley
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Takes a directory name and removes any illegal characters and shortens
     * it to 64 characters if needed.
     * @param name The name of the directory to clean.
     * @return The sanitized directory name.
     */
    public static String sanitizeDir(String name) {
        // Remove illegal characters
        name = name.replaceAll("[\\\\/:*?\\\"<>|"// Invalid folder chars
                + "\\x00-\\x1F]", // Control characters
                "");

        // Remove trailing white space
        name = name.trim();

        // Shorten to 64 characters in length
        if (name.length() > 64)
            name = name.substring(0, 64);

        return name;
    }
}

Related

  1. sanitizeClassname(String classname)
  2. sanitizeClassName(String s)
  3. sanitizeColumn(String colName, String relationName)
  4. sanitizeCompletion(String replace)
  5. sanitizeDescription(String description)
  6. sanitizeFolderName(String s)
  7. sanitizeForCmisName(String in)
  8. sanitizeForCsv(String str)
  9. sanitizeForJson(String data)