Java String Remove removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase)

Here you can find the source of removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase)

Description

remove Extra Spaces And Special Characters

License

Open Source License

Declaration

public static char[] removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.// ww  w  .j av a 2s.  c  om
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    private static char[] PUNCTUATION = null;

    public static char[] removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase) {
        boolean lastCharacterAddedWasWhiteSpace = false;
        StringBuilder searchString = new StringBuilder();
        for (int index = 0; index < toSearch.length(); index++) {
            char currChar = toSearch.charAt(index);
            if (setAllToLowerCase) {
                currChar = Character.toLowerCase(currChar);
            }
            if (currChar != '\r' && currChar != '\n') {
                if (isPunctuationOrApostrophe(currChar)) {
                    currChar = ' ';
                }
                if (Character.isWhitespace(currChar)) {
                    if (!lastCharacterAddedWasWhiteSpace) {
                        searchString.append(currChar);
                        lastCharacterAddedWasWhiteSpace = true;
                    }
                } else {
                    searchString.append(currChar);
                    lastCharacterAddedWasWhiteSpace = false;
                }
            }
        }
        return searchString.toString().trim().toCharArray();
    }

    public static boolean isPunctuationOrApostrophe(char character) {
        return Arrays.binarySearch(PUNCTUATION, character) > 0 || character == '\'';
    }
}

Related

  1. removeAndConcatenateAlternate(String s1, String s2)
  2. removeBlankLines(String s)
  3. removeCommandFromString(String commandString)
  4. removeEmpties(final String... values)
  5. removeEscape(String s_)
  6. removeField(BSONObject b, String fieldName)
  7. removeFillers(String str)
  8. removeFromSearchPath(String _path)
  9. removeGender(String pos)