package com.yeshi.appupdate.util;
|
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.text.DecimalFormat;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.StringTokenizer;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import android.content.Context;
|
import android.content.pm.PackageManager;
|
import android.os.Build;
|
import androidx.core.content.ContextCompat;
|
import android.telephony.TelephonyManager;
|
import android.text.Spannable;
|
import android.text.SpannableStringBuilder;
|
import android.text.style.ForegroundColorSpan;
|
import android.text.style.RelativeSizeSpan;
|
import android.widget.TextView;
|
|
import com.lcjian.library.util.common.DeviceUtil;
|
|
/**
|
* 瀛楃蹇嵎鏂瑰紡锛氬垽鏂瓧绗︿覆<涓枃瀛楃銆侀偖绠便?鎵嬫満鍙枫?绌哄瓧绗︺?鏁存暟銆佹诞鐐规暟>
|
*/
|
public class StringUtils {
|
public static final String EMPTY_STRING = ""; // 绌哄瓧绗︿覆
|
|
public static String join(String[] strs) {
|
StringBuilder result = new StringBuilder();
|
if (strs != null) {
|
for (String str : strs) {
|
result.append(str).append(",");
|
}
|
}
|
if (result.length() > 0) {
|
return result.substring(0, result.length() - 1);
|
}
|
return "";
|
}
|
|
// 瑙f瀽鐭俊鎺ㄩ?鍐呭
|
public static Map<String, Object> getParameterMap(String data) {
|
Map<String, Object> map = null;
|
if (data != null) {
|
map = new HashMap<String, Object>();
|
String[] params = data.split("&");
|
for (int i = 0; i < params.length; i++) {
|
int idx = params[i].indexOf("=");
|
if (idx >= 0) {
|
map.put(params[i].substring(0, idx),
|
params[i].substring(idx + 1));
|
}
|
}
|
}
|
return map;
|
}
|
|
// 妫?祴瀛楃涓叉槸鍚︾鍚堢敤鎴峰悕
|
public static boolean checkingMsg(int len) {
|
boolean isValid = true;
|
if (5 < len && len < 21) {
|
isValid = false;
|
} else {
|
isValid = true;
|
}
|
return isValid;
|
}
|
|
// 妫?祴
|
public static boolean isVaild(int len) {
|
boolean isValid = true;
|
if (1 < len && len < 17) {
|
isValid = false;
|
}
|
return isValid;
|
}
|
|
// 鍒ゆ柇瀛楃涓叉槸鍚︽槸鏁存暟
|
public static boolean isInteger(String aString) {
|
try {
|
Integer.parseInt(aString);
|
return true;
|
} catch (NumberFormatException e) {
|
return false;
|
}
|
}
|
|
// 鍒ゆ柇瀛楃涓叉槸鍚︽槸娴偣鏁?
|
public static boolean isDouble(String value) {
|
try {
|
Double.parseDouble(value);
|
if (value.contains("."))
|
return true;
|
return false;
|
} catch (NumberFormatException e) {
|
return false;
|
}
|
}
|
|
// 妫?祴瀛楃涓叉槸鍚︿负涓枃瀛楃
|
public static boolean isChinesrChar(String str) {
|
if (str.length() < str.getBytes().length) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
// 鍒ゆ柇瀛楃涓叉槸鍚︿负閭
|
public static boolean isEmailVaild(String aEmail) {
|
boolean isValid = true;
|
Pattern pattern = Pattern
|
.compile(
|
"^([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$",
|
Pattern.CASE_INSENSITIVE);
|
Matcher matcher = pattern.matcher(aEmail);
|
if (matcher.matches()) {
|
isValid = false;
|
}
|
return isValid;
|
}
|
|
// 鍒ゆ柇瀛楃涓叉槸鍚︿负鎵嬫満鍙风爜
|
public static boolean isMobileNumber(String aTelNumber) {
|
Pattern p = Pattern
|
.compile("(^1((((3[5-9])|(47)|(5[0-2])|(5[7-9])|(82)|(8[7-8]))\\d{8})|((34[0-8])\\d{7}))$)|(^1((3[0-2])|(5[5-6])|(8[0-6]))\\d{8}$)|(^1((33[0-9])|(349)|(53[0-9])|(80[0-9])|(89[0-9]))\\d{7}$)");
|
Matcher m = p.matcher(aTelNumber);
|
return m.matches();
|
}
|
|
// 鏍煎紡鍖栨墜鏈哄彿鐮?
|
public static String formatPhoneNum(String aPhoneNum) {
|
String first = aPhoneNum.substring(0, 3);
|
String end = aPhoneNum.substring(7, 11);
|
String phoneNumber = first + "****" + end;
|
return phoneNumber;
|
}
|
|
// 妫?煡瀛楃涓叉槸鍚︿负绾暟瀛?
|
public static boolean isNumeric(String str) {
|
for (int i = str.length(); --i >= 0;) {
|
if (!Character.isDigit(str.charAt(i))) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
public static boolean isLetter(String s) {
|
for (int i = 0; i < s.length(); i++) {
|
if (!(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
|
&& !(s.charAt(i) >= 'a' && s.charAt(i) <= 'z')) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
// 鍘婚櫎瀛楃涓蹭腑绌烘牸
|
public static String clearSpaces(String aString) {
|
StringTokenizer aStringTok = new StringTokenizer(aString, " ", false);
|
String aResult = "";
|
while (aStringTok.hasMoreElements()) {
|
aResult += aStringTok.nextElement();
|
}
|
return aResult;
|
}
|
|
/**
|
* is null or its length is 0 or it is made by space
|
*
|
* @param str
|
* @return if string is null or its size is 0 or it is made by space, return
|
* true, else return false.
|
*/
|
public static boolean isBlank(String str) {
|
return (str == null || str.trim().length() == 0);
|
}
|
|
/**
|
* is null or its length is 0
|
*
|
* @param str
|
* @return if string is null or its size is 0, return true, else return
|
* false.
|
*/
|
public static boolean isEmpty(String str) {
|
return (str == null || str.length() == 0);
|
}
|
|
/**
|
* compare two string
|
*
|
* @param actual
|
* @param expected
|
* @return
|
* @see ObjectUtils#isEquals(Object, Object)
|
*/
|
// public static boolean isEquals(String actual, String expected) {
|
// return ObjectUtils.isEquals(actual, expected);
|
// }
|
|
/**
|
* capitalize first letter
|
*
|
* <pre>
|
* capitalizeFirstLetter(null) = null;
|
* capitalizeFirstLetter("") = "";
|
* capitalizeFirstLetter("2ab") = "2ab"
|
* capitalizeFirstLetter("a") = "A"
|
* capitalizeFirstLetter("ab") = "Ab"
|
* capitalizeFirstLetter("Abc") = "Abc"
|
* </pre>
|
*
|
* @param str
|
* @return
|
*/
|
public static String capitalizeFirstLetter(String str) {
|
if (isEmpty(str)) {
|
return str;
|
}
|
char c = str.charAt(0);
|
return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str
|
: new StringBuilder(str.length())
|
.append(Character.toUpperCase(c))
|
.append(str.substring(1)).toString();
|
}
|
|
|
public static boolean isInt(String text) {
|
if (text == null || text.length() == 0) {
|
return false;
|
}
|
try {
|
Integer.parseInt(text);
|
return true;
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
public static final String[] imageEndWiths = new String[] { ".jpg", ".gif",
|
".png", ".bmp" };
|
|
public static boolean isEmail(String email) {
|
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
|
Pattern p = Pattern.compile(regex);
|
Matcher m = p.matcher(email);
|
return m.find();
|
}
|
|
/**
|
* 是否是正确的电话号码
|
*
|
* @param mobile
|
* @return
|
*/
|
public static boolean isMobile(String mobile) {
|
|
String regex = "^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
|
Pattern p = Pattern.compile(regex);
|
Matcher m = p.matcher(mobile);
|
|
if (mobile == null || mobile.equals("") || mobile.length() != 11) {
|
|
return false;
|
|
} else {
|
return m.find();
|
}
|
}
|
|
public static boolean isVoice(String text) {
|
|
return text.toLowerCase().endsWith(".amr");
|
|
}
|
|
public static boolean isImage(String text) {
|
text = text.toLowerCase();
|
for (int i = 0; i < imageEndWiths.length; i++) {
|
String endWidth = imageEndWiths[i];
|
if (text.endsWith(endWidth)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public static String createNewFileName(String fileName) {
|
|
int i = fileName.lastIndexOf(".");
|
|
String end = fileName.substring(i, fileName.length());
|
|
return System.currentTimeMillis() + end;
|
|
}
|
|
public static boolean isTrimEmpty(String text) {
|
if (text == null || text.trim().length() == 0) {
|
return true;
|
}
|
return false;
|
}
|
|
public static boolean isNullOrEmpty(String text) {
|
if (text == null || text.length() == 0 || text.equals("null")) {
|
return true;
|
}
|
return false;
|
}
|
|
public static String itrim(String beginTimeDis) {
|
if (beginTimeDis != null) {
|
int length = beginTimeDis.length();
|
StringBuffer buffer = new StringBuffer();
|
for (int i = 0; i < length; i++) {
|
char c = beginTimeDis.charAt(i);
|
if (c != ':') {
|
buffer.append(c);
|
} else {
|
buffer.append(":");
|
}
|
}
|
return buffer.toString();
|
}
|
|
return null;
|
}
|
|
public static String getLocationDis(double distance) {
|
if (distance < 0) {
|
return "0m";
|
} else if (distance < 100) {
|
int d = (int) distance;
|
return d + "m";
|
} else {
|
String desc = "";
|
|
double f = distance / 1000d;
|
|
DecimalFormat df = new DecimalFormat("#.###");
|
|
desc = df.format(f);
|
|
return desc + "km";
|
|
}
|
|
}
|
|
public static int toInt(String nextText, int i) {
|
try {
|
int value = Integer.parseInt(nextText);
|
return value;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return i;
|
}
|
|
/**
|
* 身份证验�?
|
*
|
* @param card
|
* @return
|
*/
|
public static boolean isIdCard(String card) {
|
Pattern p = Pattern.compile("^(\\d{14}|\\d{17})(\\d|[xX])$");
|
Matcher matcher = p.matcher(card);
|
return matcher.matches();
|
}
|
|
public static boolean isCharecter(String str) {
|
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#�?…�?&*()—�?+|{}【�?‘;:�?“�?。,、?]";
|
Pattern p = Pattern.compile(regEx);
|
Matcher m = p.matcher(str);
|
return m.find();
|
}
|
|
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
|
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
private static String toHexString(byte[] b) {
|
// String to byte
|
StringBuilder sb = new StringBuilder(b.length * 2);
|
for (int i = 0; i < b.length; i++) {
|
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
|
sb.append(HEX_DIGITS[b[i] & 0x0f]);
|
}
|
return sb.toString();
|
}
|
|
/*
|
* MD5加密
|
*/
|
public static String MD5(String s) {
|
try {
|
// Create MD5 Hash
|
MessageDigest digest = java.security.MessageDigest
|
.getInstance("MD5");
|
digest.update(s.getBytes());
|
byte messageDigest[] = digest.digest();
|
|
return toHexString(messageDigest);
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
}
|
|
return "";
|
}
|
|
public static String getSimpleString(String st) {
|
if (isNullOrEmpty(st)) {
|
return "暂无";
|
}
|
return st;
|
}
|
|
// 判断List是否为空
|
|
public static boolean listIsNullOrEmpty(List<String> list) {
|
if (list == null || list.size() == 0)
|
return true;
|
else
|
return false;
|
}
|
|
// 获取手机型号
|
public static String PeopleModel() {
|
Build bd = new Build();
|
String model = bd.MODEL;
|
return model;
|
};
|
|
public static String PeopleDeviceId(Context context) {
|
TelephonyManager tm = (TelephonyManager) context
|
.getSystemService(Context.TELEPHONY_SERVICE);
|
String deviceid ="";
|
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
|
|| ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
|
deviceid = DeviceUtil.getImeiCache(context);
|
}
|
|
return deviceid;
|
}
|
|
public static void getdiffrentColor(TextView tv, int color, int start,
|
int end) {
|
SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
|
ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
|
style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
tv.setText(style);
|
}
|
|
public static void getdiffrentColor(TextView tv, int color, int size,
|
int start, int end) {
|
SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
|
ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
|
style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
RelativeSizeSpan sizeSpan = new RelativeSizeSpan(size);
|
style.setSpan(sizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
tv.setText(style);
|
}
|
|
public static String get2Number(int number) {
|
if (number >= 0 && number < 10)
|
return "0" + number;
|
else
|
return number + "";
|
}
|
|
|
}
|