Here you can find the source of removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase)
public static char[] removeExtraSpacesAndSpecialCharacters(String toSearch, boolean setAllToLowerCase)
//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 == '\''; } }