package com.hanju.video.app.util.downutil;
|
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.text.DecimalFormat;
|
import java.util.List;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import android.content.Context;
|
import android.graphics.Color;
|
import android.os.Build;
|
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;
|
|
public class StringUtils {
|
|
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);
|
|
return System.currentTimeMillis() + end;
|
|
}
|
|
public static boolean isTrimEmpty(String text) {
|
return text == null || text.trim().length() == 0;
|
}
|
|
public static boolean isNullOrEmpty(String text) {
|
return text == null || text.length() == 0 || text.equals("null");
|
}
|
|
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) {
|
return list == null || list.size() == 0;
|
}
|
|
// 获取手机型号
|
public static String PeopleModel() {
|
Build bd = new Build();
|
String model = Build.MODEL;
|
return model;
|
}
|
|
public static String PeopleDeviceId(Context context) {
|
TelephonyManager tm = (TelephonyManager) context
|
.getSystemService(Context.TELEPHONY_SERVICE);
|
String deviceid = tm.getDeviceId();
|
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 + "";
|
}
|
|
}
|