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