Java File Name Clean cleanFileName(final String value)

Here you can find the source of cleanFileName(final String value)

Description

clean File Name

License

Apache License

Declaration

public static String cleanFileName(final String value) 

Method Source Code

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

public class Main {
    public static String cleanFileName(final String value) {
        assert value != null;
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
            final char ch = value.charAt(i);
            if ((ch >= 'A') && (ch <= 'z')) {
                sb.append(ch);//from w  w  w .  j  a  va  2s.c o  m
            }
            if ((ch >= '0') && (ch <= '9')) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    /**
     * Extends or truncate string to length if necessary and add to the string
     * builder.
     * 
     * @param sb
     * @param str
     * @param length
     */
    public static void append(final StringBuilder sb, final String str, final int length) {
        assert sb != null;
        if (str == null) {
            for (int i = 0; i < length; i++) {
                sb.append(' ');
            }
        } else {
            final int sl = str.length();
            if (sl < length) {
                sb.append(str);
                for (int i = sl; i < length; i++) {
                    sb.append(' ');
                }
            } else {
                sb.append(str.subSequence(0, length));
            }
        }

    }
}

Related

  1. cleanFileName(final String originalFileName)
  2. cleanFileName(String arg)
  3. cleanFileName(String badFileName)
  4. cleanFilename(String filename)
  5. cleanFileName(String filename)