Java File Name Clean cleanFileName(String s)

Here you can find the source of cleanFileName(String s)

Description

Make a string safely usable as a file name by removing all illegal characters (and a few more).

License

Open Source License

Parameter

Parameter Description
s A string that is supposed to be used as a file name and therefore cleaned from illegal characters.

Return

Sanitized string

Declaration

public static String cleanFileName(String s) 

Method Source Code

//package com.java2s;
/*/* w  w  w .j  a v a2s.c o m*/
 * Copyright 2004 - 2013 Wayne Grant
 *           2013 - 2016 Kai Kramer
 *
 * This file is part of KeyStore Explorer.
 *
 * KeyStore Explorer 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.
 *
 * KeyStore Explorer 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 KeyStore Explorer.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Make a string safely usable as a file name by removing all illegal characters (and a few
     * more).
     *
     * @param s
     *            A string that is supposed to be used as a file name and therefore cleaned from
     *            illegal characters.
     * @return Sanitized string
     */
    public static String cleanFileName(String s) {
        return s.replaceAll("[^a-zA-Z0-9\\._]+", "_");
    }
}

Related

  1. cleanFileName(String filename)
  2. cleanFilename(String filename)
  3. cleanFileName(String name)
  4. cleanFileName(String name)
  5. cleanFileName(String path)
  6. cleanFileName(String sText)
  7. cleanFilename(String typeName)