Here you can find the source of sanitizeFileName(String s)
Parameter | Description |
---|---|
s | Incoming string to be sanitized. |
public static String sanitizeFileName(String s)
//package com.java2s; /******************************************************************************* * Copyright (c) 2012 Firestar Software, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from www.j ava2s .c om*/ * Firestar Software, Inc. - initial API and implementation * * Author: * Gabriel Oancea * *******************************************************************************/ public class Main { private static final String INVALID_FILE_NAME_CHARS = "\\|/:*?\"<>,'`; "; /** * Return a valid file name, replacing all invalid characters with '_'. * * @param s Incoming string to be sanitized. * @return A valid file name. */ public static String sanitizeFileName(String s) { if (s == null || s.length() <= 0) return s; StringBuffer sb = new StringBuffer(s); for (int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); if (INVALID_FILE_NAME_CHARS.indexOf(c) >= 0) sb.setCharAt(i, '_'); } return sb.toString(); } }