mac
2024-03-20 07ef58effb8a570cd8a917e5a0c4d2b57e0fd5d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.hdl.photovoltaic.utils;
 
import android.text.TextUtils;
 
import com.zxing.utils.Strings;
 
import java.util.Iterator;
import java.util.regex.Pattern;
 
public class StringUtils {
    public static final String EMPTY = "";
    public static final String BLANK = " ";
    public static final String EQUAL = "=";
    public static final String AND = "&";
    public static final String QMARK = "?";
    public static final String TRUE = "true";
    public static final String FALSE = "false";
    public static final String CANCEL = "cancel";
    public static final String NULL_STR = "null";
    public static final String SUCCESS = "success";
    public static final String FAIL = "fail";
    public static final String ZERO = "0";
    public static final String SEMICOLON = ";";
    public static final String SPLITE = "/";
    public static final String AT = "@";
    public static final String COMMA = ",";
    public static final String FILE_PRE = "file://";
    public static final String HTTP_PRE = "http";
    public static final String COLON = ":";
    public static final String WRAP = "";
    public static final String STAR = "*";
    public static final String MORE = "...";
 
    /**
     * 去除字符串中的空格
     */
    public static String trimAll(CharSequence s) {
        if (s == null || s.length() == 0) return Strings.EMPTY;
        StringBuilder sb = new StringBuilder(s.length());
        for (int i = 0, L = s.length(); i < L; i++) {
            char c = s.charAt(i);
            if (c != ' ') sb.append(c);
        }
        return sb.toString();
    }
 
    /**
     * 判断是否为手机号码
     */
    public static boolean isPhoneNum(String phoneNum) {
        if (phoneNum == null || phoneNum.length() != 11) return false;
        if (!TextUtils.isDigitsOnly(phoneNum)) return false;
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$");
        return p.matcher(phoneNum).find();
    }
 
    /**
     * 判断是否为座机号码
     */
    public static boolean isTelNum(String num) {
        if (TextUtils.isEmpty(num)) return false;
        Pattern p = Pattern.compile("(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{8}");
        return p.matcher(num).find();
    }
 
    /**
     * 判断字符串中是否有中文,有中文返回true
     */
    public static boolean isChinese(String string) {
        boolean flag = false;
        for (int i = 0, L = string.length(); i < L; i++) {
            char c = string.charAt(i);
            if ((c >= 0x4e00) && (c <= 0x9FA5)) {
                flag = true;
            } else {
                return false;
            }
        }
        return flag;
    }
 
    /**
     * 判断字符长度是否在范围里面。 当start end 为 -1 时,表示字符长度不考虑上线或下线
     *
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static boolean isLengthInRange(String str, int start, int end) {
        boolean isInRange = true;
        int length = str.length();
        if (start != -1 && length < start) {
            isInRange = false;
        }
        if (end != -1 && end < length) {
            isInRange = false;
        }
        return isInRange;
    }
 
    /**
     * 字符串转换为long型
     */
    public static long toLong(String text, long defaultVal) {
        if (TextUtils.isDigitsOnly(text)) {
            try {
                return Long.parseLong(text);
            } catch (NumberFormatException e) {
            }
        }
        return defaultVal;
    }
 
    /**
     * 字符串转换为int
     */
    public static int toInt(String text, int defaultVal) {
        if (TextUtils.isDigitsOnly(text)) {
            try {
                return Integer.parseInt(text);
            } catch (NumberFormatException e) {
            }
        }
        return defaultVal;
    }
 
 
    public static String join(Iterable<?> iterable, String separator) {
 
        // handle null, zero and one elements before building a buffer
        if (iterable == null) {
            return null;
        }
        Iterator<?> iterator = iterable.iterator();
        if (!iterator.hasNext()) {
            return EMPTY;
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return toString(first);
        }
 
        // two or more elements
        StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
        if (first != null) {
            buf.append(first);
        }
 
        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }
 
    public static String toString(Object obj) {
        return obj == null ? "" : obj.toString();
    }
 
    /**
     * 判断是否为字符串是否为空
     */
    public static boolean toBoolean(String property, boolean defaultVal) {
        return property == null ? defaultVal : Boolean.valueOf(property);
    }
 
    public static String format(String str, Object... obj) {
        try {
            return String.format(str, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}