package com.wpc.library.util.common;
|
|
import android.text.Editable;
|
import android.util.Log;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.text.DecimalFormat;
|
import java.util.ArrayList;
|
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;
|
|
|
/**
|
* 字符快捷方式:判断字符串<中文字符、邮箱、手机号、空字符、整数、浮点数>
|
*/
|
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 "";
|
}
|
|
// 解析短信推送内容
|
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;
|
}
|
|
/**
|
* 输入流转化为字符串
|
*
|
* @param is
|
* @return
|
*/
|
public static String convertStreamToString(InputStream is) {
|
/*
|
* To convert the InputStream to String we use the BufferedReader.readLine()
|
* method. We iterate until the BufferedReader return null which means
|
* there's no more data to read. Each line will appended to a StringBuilder
|
* and returned as String.
|
*/
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
StringBuilder sb = new StringBuilder();
|
|
String line = null;
|
try {
|
while ((line = reader.readLine()) != null) {
|
sb.append(line + "\n");
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
is.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
return sb.toString();
|
}
|
|
// 检测字符串是否符合用户名
|
public static boolean checkingMsg(int len) {
|
boolean isValid = true;
|
isValid = !(5 < len && len < 21);
|
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 String stringToDoubleStr(String str) {
|
double dou = Double.parseDouble(str);
|
DecimalFormat df = new DecimalFormat("0.00");
|
str = df.format(dou);
|
return str;
|
}
|
|
// 判断字符串是否是浮点数
|
public static boolean isDouble(String value) {
|
try {
|
Double.parseDouble(value);
|
return value.contains(".");
|
} catch (NumberFormatException e) {
|
return false;
|
}
|
}
|
|
// 检测字符串是否为中文字符
|
public static boolean isChinesrChar(String str) {
|
return str.length() < str.getBytes().length;
|
}
|
|
// 判断字符串是否为邮箱
|
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) {
|
String regex = "^((13[0-9])|(14[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9])|(19[0-9])|(16[0-9]))\\d{8}$";
|
Pattern p = Pattern.compile(regex);
|
Matcher m = p.matcher(aTelNumber);
|
|
if (aTelNumber == null || aTelNumber.equals("") || aTelNumber.length() != 11) {
|
|
return false;
|
|
} else {
|
return m.find();
|
}
|
}
|
|
// 格式化手机号码
|
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);
|
}
|
|
public static boolean isEmpty(Editable text) {
|
if (text == null)
|
return true;
|
if (isEmpty(text.toString()))
|
return true;
|
return false;
|
}
|
|
/**
|
* 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
|
* <p/>
|
* <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();
|
}
|
|
/**
|
* //获取完整的域名
|
*
|
* @param text 获取浏览器分享出来的text文本
|
*/
|
public static List<String> getCompleteUrl(String text) {
|
Pattern p = Pattern.compile("((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?", Pattern.CASE_INSENSITIVE);
|
Matcher matcher = p.matcher(text);
|
// matcher.find();
|
List<String> mList = new ArrayList<>();
|
while (matcher.find()) {
|
mList.add(matcher.group());
|
}
|
return mList;
|
}
|
|
}
|