001/* 002 * Copyright 2010, 2011 Christopher Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.bindings.core.utils.string; 017 018/** 019 * Utility methods for working with Strings 020 */ 021public final class StringUtils { 022 023 private StringUtils() { 024 } 025 026 /** 027 * Removes any whitespace from a String, correctly handling surrogate characters 028 * @param string String to process 029 * @return String with any whitespace removed 030 */ 031 public static String removeWhitespace(String string) { 032 033 if (string == null || string.length() == 0) { 034 return string; 035 } else { 036 int codePoints = string.codePointCount(0, string.length()); 037 StringBuilder sb = new StringBuilder(); 038 039 for (int i = 0; i < codePoints; i++) { 040 int offset = string.offsetByCodePoints(0, i); 041 042 int nextCodePoint = string.codePointAt(offset); 043 if (!Character.isWhitespace(nextCodePoint)) { 044 sb.appendCodePoint(nextCodePoint); 045 } 046 } 047 048 if (string.length() == sb.length()) { 049 return string; 050 } else { 051 return sb.toString(); 052 } 053 } 054 } 055}