Java String Strip strip(final String s)

Here you can find the source of strip(final String s)

Description

Strips undisplayable characters from the String

License

Open Source License

Parameter

Parameter Description
s the String

Return

the stripped String

Declaration

public static final String strip(final String s) 

Method Source Code

//package com.java2s;
/**/*from  ww  w  . j  a v a  2  s . c  om*/
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import java.util.*;

public class Main {
    public final static int HANDLE_UNKNOWN_EXCEPTION = 0;
    public final static int HANDLE_UNKNOWN_INCLUDE = 1;

    /**
     * Strips undisplayable characters from the String
     * 
     * @param s the String
     * @return the stripped String
     **/
    public static final String strip(final String s) {
        return strip(s, false, HANDLE_UNKNOWN_INCLUDE);
    }

    /**
     * Strips undisplayable characters from the String
     * 
     * @param s the String
     * @param clean whether known characters should be replaced with clean characters instead of
     *            stripped
     * @param handleUnknownMode when cleaning, this indicates that an Exception should be thrown for
     *            unknown characters
     * @return the stripped String
     **/
    public static final String strip(final String s, final boolean clean, final int handleUnknownMode) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = null;
        char[] chars = null;
        int start = 0;
        final int len = s.length();
        for (int i = 0; i < len; i++) {
            final char c = s.charAt(i);
            if (!isDisplayable(c)) {
                if (sb == null) {
                    sb = new StringBuilder(len);
                    chars = s.toCharArray();
                }
                sb.append(chars, start, i - start);
                if (clean) {
                    switch (c) {
                    // Used to put actual characters in comments, but that caused problems when Ubuntu users checked out and recommitted code
                    case 130:
                    case 8218:
                        sb.append(',');
                        break;
                    case 136:
                    case 710:
                        sb.append('^');
                        break;
                    case 139:
                    case 8249:
                        sb.append('<');
                        break;
                    case 145:
                    case 8216:
                    case 146:
                    case 8217:
                    case 180:
                        sb.append('\'');
                        break;
                    case 147: // Left "
                    case 8220:
                    case 148: // Right "
                    case 8221:
                        sb.append('"');
                        break;
                    case 150:
                    case 8211:
                    case 151: // Long -
                    case 8212:
                        sb.append('-');
                        break;
                    case 155:
                    case 8250:
                        sb.append('>');
                        break;
                    case 169: // Copyright
                        sb.append("(C)");
                        break;
                    case 171:
                        sb.append("<<");
                        break;
                    case 178: // 2 Exponent
                        sb.append("^2");
                        break;
                    case 179: // 3 Exponent
                        sb.append("^3");
                        break;
                    case 187:
                        sb.append(">>");
                        break;
                    case 188: // Fraction
                        sb.append("1/4");
                        break;
                    case 189: // Fraction
                        sb.append("1/2");
                        break;
                    case 190: // Fraction
                        sb.append("3/4");
                        break;
                    case 215: // Multiplication
                        sb.append('*');
                        break;
                    case 247: // Division
                        sb.append('/');
                        break;
                    default:
                        switch (handleUnknownMode) {
                        case HANDLE_UNKNOWN_EXCEPTION:
                            throw new RuntimeException("Unrecognized character " + (int) c + ": " + c);
                        case HANDLE_UNKNOWN_INCLUDE:
                            sb.append(c);
                            break;
                        }
                    }
                }
                start = i + 1;
            }
        }

        if (sb == null) {
            return s;
        } else {
            if (start < len) {
                sb.append(chars, start, len - start);
            }
            return sb.toString();
        }
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object array) {
        if (array == null) {
            return 0;
        } else if (array instanceof byte[]) {
            return ((byte[]) array).length;
        } else if (array instanceof short[]) {
            return ((short[]) array).length;
        } else if (array instanceof int[]) {
            return ((int[]) array).length;
        } else if (array instanceof long[]) {
            return ((long[]) array).length;
        } else if (array instanceof float[]) {
            return ((float[]) array).length;
        } else if (array instanceof double[]) {
            return ((double[]) array).length;
        } else if (array instanceof boolean[]) {
            return ((boolean[]) array).length;
        } else if (array instanceof char[]) {
            return ((char[]) array).length;
        }
        return ((Object[]) array).length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final byte[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final int[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Retrieves the length of the CharSequence
     * 
     * @param s the CharSequence
     * @return the length
     **/
    public final static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * Retrieves whether the character is displayable
     * 
     * @param c the character
     * @return whether the character is displayable
     **/
    public static final boolean isDisplayable(final char c) {
        return ((c > 31) && (c < 127)) || (c == 9) || (c == 10) || (c == 13);
    }

    /**
     * Appends the given character to the given StringBuffer, allocating the StringBuffer if
     * necessary
     * 
     * @param sb the StringBuffer
     * @param c the char
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final char c) {
        return (sb == null ? new StringBuffer() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuffer, allocating the StringBuffer if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final String s) {
        return s == null ? sb : (sb == null ? new StringBuffer() : sb).append(s);
    }

    /**
     * Appends part of the given CharSequence to the given StringBuffer, allocating the StringBuffer
     * if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @param offset the offset of the CharSequence at which to start copying
     * @param len the number of characters to copy
     * @return the StringBuffer
     **/
    public final static StringBuffer append(StringBuffer sb, final CharSequence s, int offset, int len) {
        if (sb == null) {
            sb = new StringBuffer(len);
        }
        sb.ensureCapacity(sb.length() + len);
        for (len += offset; offset < len; offset++) {
            sb.append(s.charAt(offset));
        }

        return sb;
    }

    /**
     * Appends the given character to the given StringBuilder, allocating the StringBuilder if
     * necessary
     * 
     * @param sb the StringBuilder
     * @param c the char
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final char c) {
        return ((sb == null) ? new StringBuilder() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuilder, allocating the StringBuilder if necessary
     * 
     * @param sb the StringBuilder
     * @param s the String
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final String s) {
        return (s == null) ? sb : ((sb == null) ? new StringBuilder() : sb).append(s);
    }

    /**
     * Converts an object to a string
     * 
     * @param o the Object to convert
     * @return the String
     **/
    public final static String toString(final Object o) {
        return o == null ? null : o.toString();
    }

    public final static <E> void ensureCapacity(final Collection<E> col, final int minCapacity) {
        if (col instanceof ArrayList) {
            ((ArrayList<E>) col).ensureCapacity(minCapacity);
        }
    }
}

Related

  1. collectionToString(Collection collection, boolean stripPackageNames)
  2. strip(String string, String token)
  3. stripBadHTMLTags(String s)
  4. stripClassFromClassName(String className)
  5. stripExtension(String filename)