1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.messaging.util; 17 18 import androidx.annotation.Nullable; 19 20 public class TextUtil { 21 /** 22 * Returns true if the string is empty, null or only whitespace. 23 */ isAllWhitespace(@ullable String string)24 public static boolean isAllWhitespace(@Nullable String string) { 25 if (string == null || string.isEmpty()) { 26 return true; 27 } 28 29 for (int i = 0; i < string.length(); ++i) { 30 if (!Character.isWhitespace(string.charAt(i))) { 31 return false; 32 } 33 } 34 35 return true; 36 } 37 38 /** 39 * Taken from PhoneNumberUtils, where it is only available in API 21+ Replaces all unicode 40 * (e.g. Arabic, Persian) digits with their decimal digit equivalents. 41 * 42 * @param number the number to perform the replacement on. 43 * @return the replaced number. 44 */ replaceUnicodeDigits(String number)45 public static String replaceUnicodeDigits(String number) { 46 StringBuilder normalizedDigits = new StringBuilder(number.length()); 47 for (char c : number.toCharArray()) { 48 int digit = Character.digit(c, 10); 49 if (digit != -1) { 50 normalizedDigits.append(digit); 51 } else { 52 normalizedDigits.append(c); 53 } 54 } 55 return normalizedDigits.toString(); 56 } 57 58 /** 59 * Appends text to the stringBuilder. 60 * If stringBuilder already has content, separator is prepended to create a separator between 61 * entries. 62 * @param stringBuilder The stringBuilder to add to 63 * @param text The text to append 64 * @param separator The separator to add if there is already text, typically "," or "\n" 65 */ appendWithSeparator(final StringBuilder stringBuilder, final String text, final String separator)66 public static void appendWithSeparator(final StringBuilder stringBuilder, final String text, 67 final String separator) { 68 if (stringBuilder.length() > 0) { 69 stringBuilder.append(separator); 70 } 71 stringBuilder.append(text); 72 } 73 } 74