Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2013 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 
     17 package android.text;
     18 
     19 public class TextUtils {
     20     private TextUtils() { /* cannot be instantiated */ }
     21 
     22     /**
     23      * Returns true if the string is null or 0-length.
     24      * @param str the string to be examined
     25      * @return true if str is null or zero length
     26      */
     27     public static boolean isEmpty(CharSequence str) {
     28         if (str == null || str.length() == 0)
     29             return true;
     30         else
     31             return false;
     32     }
     33 
     34     /**
     35      * Returns true if a and b are equal, including if they are both null.
     36      * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if
     37      * both the arguments were instances of String.</i></p>
     38      * @param a first CharSequence to check
     39      * @param b second CharSequence to check
     40      * @return true if a and b are equal
     41      */
     42     public static boolean equals(CharSequence a, CharSequence b) {
     43         if (a == b) return true;
     44         int length;
     45         if (a != null && b != null && (length = a.length()) == b.length()) {
     46             if (a instanceof String && b instanceof String) {
     47                 return a.equals(b);
     48             } else {
     49                 for (int i = 0; i < length; i++) {
     50                     if (a.charAt(i) != b.charAt(i)) return false;
     51                 }
     52                 return true;
     53             }
     54         }
     55         return false;
     56     }
     57 
     58     /**
     59      * Returns list of multiple {@link CharSequence} joined into a single
     60      * {@link CharSequence} separated by localized delimiter such as ", ".
     61      *
     62      * @hide
     63      */
     64     public static CharSequence join(Iterable<CharSequence> list) {
     65         final CharSequence delimiter = ", ";
     66         return join(delimiter, list);
     67     }
     68 
     69     /**
     70      * Returns a string containing the tokens joined by delimiters.
     71      * @param tokens an array objects to be joined. Strings will be formed from
     72      *     the objects by calling object.toString().
     73      */
     74     public static String join(CharSequence delimiter, Object[] tokens) {
     75         StringBuilder sb = new StringBuilder();
     76         boolean firstTime = true;
     77         for (Object token: tokens) {
     78             if (firstTime) {
     79                 firstTime = false;
     80             } else {
     81                 sb.append(delimiter);
     82             }
     83             sb.append(token);
     84         }
     85         return sb.toString();
     86     }
     87 
     88     /**
     89      * Returns a string containing the tokens joined by delimiters.
     90      * @param tokens an array objects to be joined. Strings will be formed from
     91      *     the objects by calling object.toString().
     92      */
     93     public static String join(CharSequence delimiter, Iterable tokens) {
     94         StringBuilder sb = new StringBuilder();
     95         boolean firstTime = true;
     96         for (Object token: tokens) {
     97             if (firstTime) {
     98                 firstTime = false;
     99             } else {
    100                 sb.append(delimiter);
    101             }
    102             sb.append(token);
    103         }
    104         return sb.toString();
    105     }
    106 
    107 }
    108