Java String Remove removePunctuation(String value)

Here you can find the source of removePunctuation(String value)

Description

remove Punctuation

License

Open Source License

Declaration

public static String removePunctuation(String value) 

Method Source Code

//package com.java2s;
/* MarcWorx MARC Library - Utilities for manipulation of MARC records
 Copyright (C) 2013  Todd Walker, Talwood Solutions

 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.//  w  w  w .java 2  s . c  o m

 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/>.
 */

import java.util.Arrays;

public class Main {
    public final static char DECIMAL_POINT = 0x002E;

    public static String removePunctuation(String value) {
        return removePunctuation(value, null);
    }

    public static String removePunctuation(String value, char exceptions[]) {
        if (value == null) {
            return "";
        }

        if (exceptions != null) {
            // Must not assume we can modify the character array passed into us
            // must make a copy....
            char copy[] = new char[exceptions.length];
            System.arraycopy(exceptions, 0, copy, 0, exceptions.length);
            exceptions = copy;
            Arrays.sort(exceptions);
        }

        boolean exception = false;
        StringBuilder filteredBuffer = new StringBuilder(value.length());

        for (int i = 0; i < value.length(); i++) {
            char testChar = value.charAt(i);

            if (exceptions != null) {
                exception = (Arrays.binarySearch(exceptions, testChar) >= 0);
            }

            if (exception || Character.isLetterOrDigit(testChar)
                    || testChar == DECIMAL_POINT
                    || Character.isWhitespace(testChar)) {
                filteredBuffer.append(testChar);
            }
        }
        return filteredBuffer.toString();
    }

    public static int length(String source) {
        int result = 0;
        if (isNotEmpty(source)) {
            result = source.length();
        }
        return result;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related

  1. removeNewLineAndTab(String value)
  2. removePostfixedNewline(String s)
  3. removeProperty(String name)
  4. removePropertyNameModifier(String name)
  5. removePunctuation(String str)
  6. removeRecursive(Object json, String keyToRemove)
  7. removeSomeOne(String words, int count)
  8. removeSpaces(String orig)
  9. removeSpecialChars(final String word)