123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125 |
- package com.wdkl.callingmainnurse.util;
- import android.content.Context;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.os.Bundle;
- import com.wdkl.callingmainnurse.BuildConfig;
- import com.wdkl.callingmainnurse.MyApplication;
- import com.wdkl.callingmainnurse.R;
- import com.wdkl.callingmainnurse.common.Constants;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Collection;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import serialporttest.utils.SerialPortUtil;
- /**
- * 类描述:字符串操作工具包
- * 创建人:Waderson Shll (TEL:15675117662)<br>
- * 创建时间:2017-11-2 <br>
- * 特别提醒:如有需要该类可任意调用;在未通知本人的情况下该类禁止任何修改!<br>
- *
- * @version 1.0
- */
- public class StringUtils {
- private final static Pattern emailer = Pattern
- .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
- private final static Pattern backCard = Pattern
- .compile(" /^(\\[0-9]{16}|\\[0-9]{18}|\\[0-9]{19})$/;");
- // private final static SimpleDateFormat dateFormater = new
- // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // private final static SimpleDateFormat dateFormater2 = new
- // SimpleDateFormat("yyyy-MM-dd");
- private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- }
- };
- private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- return new SimpleDateFormat("yyyy-MM-dd");
- }
- };
- public static String getAppVersionName(Context context) {
- try {
- PackageInfo info = context.getPackageManager().getPackageInfo(
- context.getPackageName(), 0);
- return info.versionName;
- // return info.versionCode;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- return 1.0 + "";
- }
- public static String getAllVersionName() {
- String str = "\r\nApp名称: " + MyApplication.getAppContext().getString(R.string.app_name)
- + "\r\nAPP版本号: V" + BuildConfig.VERSION_NAME
- + "\r\nMCU版本号: " + Constants.MCU_VERSION_NUMBER
- + "\r\n设备ID: " + SerialPortUtil.KEY_ID
- + "\r\n中央服务器系列"
- + "\r\n发布日期:" + BuildConfig.BUILD_TIME;
- if (android.os.Build.VERSION.SDK_INT >= 26) { //android8.0 = 26
- str = str + " android8.1 , rk3368";// rk3368 android8.1
- } else if (android.os.Build.VERSION.SDK_INT >= 24) { //android7.0 = 24
- str = str + " android7.1 , rk3128";//rk3128 android7.1
- } else {
- str = str + " android4.2 , a20";// a20 android4.2
- }
- return str;
- }
- /**
- * 将字符串转为日期类型
- *
- * @param sdate
- * @return
- */
- public static Date toDate(String sdate) {
- try {
- return dateFormater.get().parse(sdate);
- } catch (ParseException e) {
- return null;
- }
- }
- /**
- * 以友好的方式显示时间
- *
- * @param sdate
- * @return
- */
- public static String friendly_time(String sdate) {
- Date time = toDate(sdate);
- if (time == null) {
- return "Unknown";
- }
- String ftime = "";
- Calendar cal = Calendar.getInstance();
- // 判断是否是同一天
- String curDate = dateFormater2.get().format(cal.getTime());
- String paramDate = dateFormater2.get().format(time);
- if (curDate.equals(paramDate)) {
- int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
- if (hour == 0)
- ftime = Math.max(
- (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
- + "分钟前";
- else
- ftime = hour + "小时前";
- return ftime;
- }
- long lt = time.getTime() / 86400000;
- long ct = cal.getTimeInMillis() / 86400000;
- int days = (int) (ct - lt);
- if (days == 0) {
- int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
- if (hour == 0)
- ftime = Math.max(
- (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
- + "分钟前";
- else
- ftime = hour + "小时前";
- } else if (days == 1) {
- ftime = "昨天";
- } else if (days == 2) {
- ftime = "前天";
- } else if (days > 2 && days <= 10) {
- ftime = days + "天前";
- } else if (days > 10) {
- ftime = dateFormater2.get().format(time);
- }
- return ftime;
- }
- /**
- * 判断给定字符串时间是否为今日
- *
- * @param sdate
- * @return boolean
- */
- public static boolean isToday(String sdate) {
- boolean b = false;
- Date time = toDate(sdate);
- Date today = new Date();
- if (time != null) {
- String nowDate = dateFormater2.get().format(today);
- String timeDate = dateFormater2.get().format(time);
- if (nowDate.equals(timeDate)) {
- b = true;
- }
- }
- return b;
- }
- /**
- * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
- *
- * @param input
- * @return boolean
- */
- public static boolean isEmpty(String input) {
- if (input == null || "".equals(input))
- return true;
- for (int i = 0; i < input.length(); i++) {
- char c = input.charAt(i);
- if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
- return false;
- }
- }
- return true;
- }
- public static boolean notEmpty(String input) {
- if (input != null && input.length() > 0
- && !input.equals("null") && !input.equals("NULL")) {
- return true;
- }
- return false;
- }
- public static boolean listNotEmpty(List list) {
- if (list != null && list.size() > 0) {
- return true;
- }
- return false;
- }
- /**
- * 判断是不是一个合法的银行卡
- *
- * @param cardNum
- * @return
- */
- public static boolean isCardNum(String cardNum) {
- if (cardNum == null || cardNum.trim().length() == 0)
- return false;
- return backCard.matcher(cardNum).matches();
- }
- /**
- * 判断是不是一个合法的电子邮件地址
- *
- * @param email
- * @return
- */
- public static boolean isEmail(String email) {
- if (email == null || email.trim().length() == 0)
- return false;
- return emailer.matcher(email).matches();
- }
- /**
- * 字符串转整数
- *
- * @param str
- * @param defValue
- * @return
- */
- public static int toInt(String str, int defValue) {
- try {
- return Integer.parseInt(str);
- } catch (Exception e) {
- }
- return defValue;
- }
- /**
- * 检查是否是金额(12.00)
- *
- * @param phone
- * @return
- */
- public static boolean checkNumEx(String phone) {
- Pattern pattern = Pattern.compile("^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
- Matcher match = pattern.matcher(phone);
- return match.matches();
- }
- /**
- * 对象转整数
- *
- * @param obj
- * @return 转换异常返回 0
- */
- public static int toInt(Object obj) {
- if (obj == null)
- return 0;
- return toInt(obj.toString(), 0);
- }
- /**
- * 对象转整数
- *
- * @param obj
- * @return 转换异常返回 0
- */
- public static long toLong(String obj) {
- try {
- return Long.parseLong(obj);
- } catch (Exception e) {
- }
- return 0;
- }
- /**
- * 字符串转布尔值
- *
- * @param b
- * @return 转换异常返回 false
- */
- public static boolean toBool(String b) {
- try {
- return Boolean.parseBoolean(b);
- } catch (Exception e) {
- }
- return false;
- }
- /**
- * 是否是英文字
- *
- * @param c
- * @return
- */
- public static boolean isLetter(char c) {
- int k = 0x80;
- return c / k == 0 ? true : false;
- }
- /**
- * 是否是英文字
- *
- * @return
- */
- public static boolean isEnglish(String str) {
- if (!notEmpty(str)) return false;
- return str.matches("[a-zA-Z]+");
- }
- /**
- * 判断一个字符串是否包含某个字符
- *
- * @return
- */
- public static boolean isHaveStr(String old, String s) {
- if (!notEmpty(old) || !notEmpty(s)) return false;
- return old.contains(s);
- }
- /**
- * 获取英文字长度
- *
- * @param s
- * @return
- */
- public static int length(String s) {
- if (s == null)
- return 0;
- char[] c = s.toCharArray();
- int len = 0;
- int length = c.length;
- for (int i = 0; i < length; i++) {
- len++;
- if (!isLetter(c[i])) {
- len++;
- }
- }
- return len;
- }
- /**
- * 去掉html标签
- *
- * @param htmlStr
- * @return
- */
- public static String delHTMLTag(String htmlStr) {
- String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
- String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
- String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
- Pattern p_script = Pattern.compile(regEx_script,
- Pattern.CASE_INSENSITIVE);
- Matcher m_script = p_script.matcher(htmlStr);
- htmlStr = m_script.replaceAll(""); // 过滤script标签
- Pattern p_style = Pattern
- .compile(regEx_style, Pattern.CASE_INSENSITIVE);
- Matcher m_style = p_style.matcher(htmlStr);
- htmlStr = m_style.replaceAll(""); // 过滤style标签
- Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
- Matcher m_html = p_html.matcher(htmlStr);
- htmlStr = m_html.replaceAll(""); // 过滤html标签
- htmlStr = htmlStr.replaceAll(" ", "");
- return htmlStr.trim(); // 返回文本字符串
- }
- /**
- * 去掉换行符
- *
- * @param str
- * @return
- */
- public static String removeLineChar(String str) {
- if (str == null)
- return "";
- return str.replaceAll("\r\n", "").replaceAll("\n", "");
- }
- /**
- * 以中文字长度计算,截取字符串
- *
- * @param origin
- * @param len
- * @param c
- * @return
- */
- public static String substring(String origin, int len, String c) {
- if (origin == null || origin.equals("") || len < 1)
- return "";
- String temp = removeLineChar(origin);
- byte[] strByte = new byte[len];
- if (len > length(origin)) {
- return temp;
- }
- try {
- System.arraycopy(temp.getBytes("GBK"), 0, strByte, 0, len);
- int count = 0;
- for (int i = 0; i < len; i++) {
- int value = (int) strByte[i];
- if (value < 0) {
- count++;
- }
- }
- if (count % 2 != 0) {
- len = (len == 1) ? ++len : --len;
- }
- return new String(strByte, 0, len, "GBK") + c;
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
- }
- public static int StrToIntDef(String s, int defaultValue) {
- int res = defaultValue;
- try {
- res = Integer.parseInt(s, 10);
- } catch (Exception e) {
- res = defaultValue;
- }
- return res;
- }
- /**
- * 将数据集合转化拼成字符串
- *
- * @param collection 集合
- * @param delimiter 分隔符
- * @return
- */
- public static String join(Collection<?> collection, String delimiter) {
- StringBuilder builder = new StringBuilder();
- Iterator<?> iter = collection.iterator();
- while (iter.hasNext()) {
- builder.append(iter.next());
- if (iter.hasNext()) {
- builder.append(delimiter);
- }
- }
- return builder.toString();
- }
- /**
- * 验证身份证是否有效15位或18位<包括对年月日的合法性进行验证>
- *
- * @param idcard
- * @return
- */
- public static boolean isIDCard(String idcard) {
- Pattern pattern = Pattern.compile("^\\d{15}(\\d{2}[0-9xX])?$");
- Matcher macher = pattern.matcher(idcard);
- if (macher.find()) {// 对年月日字符串的验证
- String power = idcard.substring(idcard.length() - 12,
- idcard.length() - 4);
- pattern = Pattern
- .compile("^[1-2]+([0-9]{3})+(0[1-9][0-2][0-9]|0[1-9]3[0-1]|1[0-2][0-3][0-1]|1[0-2][0-2][0-9])");
- macher = pattern.matcher(power);
- }
- return macher.find();
- }
- /**
- * 检查是否是正确的email
- *
- * @param email
- * @return
- */
- public static boolean checkEmail(String email) {
- String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
- Pattern p = Pattern.compile(regex);
- Matcher m = p.matcher(email);
- return m.matches();
- }
- /**
- * 验证邮箱格式
- *
- * @param strEmail
- * @return
- */
- public static boolean isEmailOther(String strEmail) {
- String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
- Pattern p = Pattern.compile(strPattern);
- Matcher m = p.matcher(strEmail);
- return m.matches();
- }
- /**
- * 判断姓名只能输入中文,或者英文
- *
- * @param name
- * @return
- */
- public static boolean isUserName(String name) {
- if (name.matches("(([\u4E00-\u9FA5]{2,7})|([a-zA-Z]{3,10}))")) {
- return true;
- }
- return false;
- }
- /**
- * 对银行卡进行加*号保护设置并格式化
- *
- * @param cardNo
- * @return 如:6222 **** **** 1234
- */
- public static String formateBankNumber(String cardNo) {
- String star = " **** **** ";
- int cardNoLength = cardNo.length();
- if (cardNoLength >= 4) {
- String start = cardNo.substring(0, 4);
- String end = cardNo.substring(cardNoLength - 4, cardNoLength);
- return start + star + end;
- } else {
- return cardNo;
- }
- }
- /**
- * 手机号码加星保护
- *
- * @param phone
- * @return 如:136*****256
- */
- public static String formatPhone(String phone) {
- String star = "******";
- return phone.substring(0, 3) + star
- + phone.substring(phone.length() - 3, phone.length());
- }
- /**
- * 检查是否是数字
- *
- * @param phone
- * @return
- */
- public static boolean checkNum(String phone) {
- Pattern pattern = Pattern.compile("[0-9]+");
- Matcher match = pattern.matcher(phone);
- return match.matches();
- }
- /**
- * 检查是否是电话号码
- */
- public static boolean isMobileNo(String paramString) {
- return Pattern.compile("^1[3|4|5|7|8]\\d{9}$").matcher(paramString)
- .matches();
- }
- /**
- * 检查用户输入账号格式是否正确,只能是邮箱或电话号码
- *
- * @param account
- * @return
- */
- public static boolean checkAccount(String account) {
- return (isMobileNo(account) || checkEmail(account));
- }
- // 校验Tag Alias 只能是数字,英文字母和中文
- public static boolean isValidTagAndAlias(String s) {
- Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
- Matcher m = p.matcher(s);
- return m.matches();
- }
- public static final String KEY_APP_KEY = "JPUSH_APPKEY";
- /**
- * 获取jpush AppKey
- *
- * @param context
- * @return
- */
- public static String getAppKey(Context context) {
- Bundle metaData = null;
- String appKey = null;
- try {
- ApplicationInfo ai = context.getPackageManager()
- .getApplicationInfo(context.getPackageName(),
- PackageManager.GET_META_DATA);
- if (null != ai)
- metaData = ai.metaData;
- if (null != metaData) {
- appKey = metaData.getString(KEY_APP_KEY);
- if ((null == appKey) || appKey.length() != 24) {
- appKey = null;
- }
- }
- } catch (NameNotFoundException e) {
- }
- return appKey;
- }
- /**
- * 将输入流转化为String
- *
- * @param is
- * @return
- */
- public static String readDataFromStream(InputStream is) {
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- StringBuilder builder = new StringBuilder();
- String line = null;
- try {
- while ((line = br.readLine()) != null) {
- builder.append(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return builder.toString();
- }
- /**
- * 补零操作
- */
- public static String zerofill(Object obj) {
- String parten = "00";
- if (obj instanceof String)
- obj = Double.parseDouble(obj.toString());
- DecimalFormat decimal = new DecimalFormat(parten);
- return decimal.format(obj);
- }
- /**
- * ASCII表中可见字符从!开始,偏移位值为33(Decimal)
- */
- static final char DBC_CHAR_START = 33; // 半角!
- /**
- * ASCII表中可见字符到~结束,偏移位值为126(Decimal)
- */
- static final char DBC_CHAR_END = 126; // 半角~
- /**
- * 全角对应于ASCII表的可见字符从!开始,偏移值为65281
- */
- static final char SBC_CHAR_START = 65281; // 全角!
- /**
- * 全角对应于ASCII表的可见字符到~结束,偏移值为65374
- */
- static final char SBC_CHAR_END = 65374; // 全角~
- /**
- * ASCII表中除空格外的可见字符与对应的全角字符的相对偏移
- */
- static final int CONVERT_STEP = 65248; // 全角半角转换间隔
- /**
- * 全角空格的值,它没有遵从与ASCII的相对偏移,必须单独处理
- */
- static final char SBC_SPACE = 12288; // 全角空格 12288
- /**
- * 半角空格的值,在ASCII中为32(Decimal)
- */
- static final char DBC_SPACE = ' '; // 半角空格
- /**
- * <PRE>
- * 半角字符->全角字符转换
- * 只处理空格,!到˜之间的字符,忽略其他
- * </PRE>
- */
- public static String bj2qj(String src) {
- if (src == null) {
- return src;
- }
- StringBuilder buf = new StringBuilder(src.length());
- char[] ca = src.toCharArray();
- for (int i = 0; i < ca.length; i++) {
- if (ca[i] == DBC_SPACE) { // 如果是半角空格,直接用全角空格替代
- buf.append(SBC_SPACE);
- } else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { // 字符是!到~之间的可见字符
- buf.append((char) (ca[i] + CONVERT_STEP));
- } else { // 不对空格以及ascii表中其他可见字符之外的字符做任何处理
- buf.append(ca[i]);
- }
- }
- return buf.toString();
- }
- /**
- * <PRE>
- * 全角字符->半角字符转换
- * 只处理全角的空格,全角!到全角~之间的字符,忽略其他
- * </PRE>
- */
- public static String qj2bj(String src) {
- if (src == null) {
- return src;
- }
- StringBuilder buf = new StringBuilder(src.length());
- char[] ca = src.toCharArray();
- for (int i = 0; i < src.length(); i++) {
- if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { // 如果位于全角!到全角~区间内
- buf.append((char) (ca[i] - CONVERT_STEP));
- } else if (ca[i] == SBC_SPACE) { // 如果是全角空格
- buf.append(DBC_SPACE);
- } else { // 不处理全角空格,全角!到全角~区间外的字符
- buf.append(ca[i]);
- }
- }
- return buf.toString();
- }
- /**
- * 字符串按索引截取
- *
- * @param str
- * @return
- */
- public static String substringByLengh(String str, int start, int end) {
- if (str == null) {
- return "";
- }
- if (start > end) {
- return "";
- }
- if (str.length() - 1 < start || str.length() < end) {
- return "";
- }
- return str.substring(start, end);
- }
- /**
- * 字符串按单个截取
- *
- * @param str
- * @return
- */
- public static ArrayList<String> substringBySing(String str) {
- if (null == str||"".equals(str)) {
- return null;
- }
- ArrayList<String> list=new ArrayList<>();
- char[] c=str.toCharArray();
- for(int i=0;i<c.length;i++) {
- String s = String.valueOf(c[i]);
- list.add(s);
- }
- return list;
- }
- /**
- * 追加内容到当前String对象<br>
- *
- * @return
- */
- public static String append(String oldStr, String appendStr) {
- if (oldStr == null) {
- return appendStr;
- }
- StringBuffer sb = new StringBuffer(oldStr);
- return sb.append(appendStr).toString();
- }
- /**
- * 删除指定位置的字符<br>
- *
- * @return
- */
- public static String deleteCharAt(String oldStr, int index) {
- if (oldStr == null) {
- return null;
- }
- if (oldStr.length() <= 0) {
- return "";
- }
- StringBuffer sb = new StringBuffer(oldStr);
- return sb.deleteCharAt(index).toString();
- }
- /**
- * String对象中插入内容<br>
- *
- * @return
- */
- public static String insert(String oldStr, String insertStr, int index) {
- if (oldStr == null) {
- return null;
- }
- StringBuffer sb = new StringBuffer(oldStr);
- return sb.insert(index, insertStr).toString();
- }
- /**
- * String对象内容反转<br>
- *
- * @return
- */
- public static String reverse(String oldStr) {
- if (oldStr == null) {
- return null;
- }
- StringBuffer sb = new StringBuffer(oldStr);
- return sb.reverse().toString();
- }
- /**
- * 修改索引值为index位置的字符<br>
- *
- * @return
- */
- public static String setCharAt(String oldStr, char charStr, int index) {
- if (oldStr == null) {
- return null;
- }
- StringBuffer sb = new StringBuffer(oldStr);
- sb.setCharAt(index, charStr);
- return sb.toString();
- }
- /**
- * 对某个字符加*操作<br>
- *
- * @return String
- */
- public static String changeTextAddsiix(String oldStr, int[] index) {
- String newStr = oldStr;
- for (int i = 0; i < index.length; i++) {
- if (index[i] < oldStr.length()) {
- newStr = setCharAt(newStr, '*', index[i]);
- }
- }
- return newStr;
- }
- /**
- * 移除所有空格<br>
- *
- * @return String
- */
- public static String replaceNull(String oldStr) {
- if (oldStr == null) {
- return null;
- }
- return oldStr.replaceAll(" ", "").replaceAll(" ", "").replaceAll("\\b", "").replaceAll("\\r", "").replaceAll("\\n", "");
- }
- /**
- * 移除某个字符<br>
- *
- * @return String
- */
- public static String replaceStr(String oldStr, CharSequence replaceStr) {
- if (oldStr == null) {
- return null;
- }
- return oldStr.replace(replaceStr, "");
- }
- /**
- * 对double类型的数据保留2位小数
- *
- * @param d
- * @return
- */
- public static double get2Decimal(double d) {
- return Math.round(d * 100) / 100d;
- // BigDecimal b = new BigDecimal(Double.toString(d));
- // return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- }
- /**
- * 对double类型的数据取整
- *
- * @param d
- * @return
- */
- public static double get0Decimal(double d) {
- String str = String.valueOf(d);
- String strNum = str.substring(0, str.lastIndexOf("."));//截取从字符串开始到小数点位置的字符串,就是整数部分
- return parseDouble(strNum);
- }
- /**
- * 判断一个字符串 能否转成一个Double类型的
- *
- * @param str
- * @return
- */
- public static boolean isDouble(String str) {
- try {
- Double.parseDouble(str);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- /**
- * 字符串转成一个Double类型的
- *
- * @param str
- * @return
- */
- public static double parseDouble(String str) {
- if (isDouble(str)) {
- return Double.parseDouble(str);
- }
- return 0.0;
- }
- /**
- * 判断一个字符串 能否转成一个long类型的
- *
- * @param str
- * @return
- */
- public static boolean isLong(String str) {
- try {
- Long.parseLong(str);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- /**
- * 字符串转成一个Long类型的
- *
- * @param str
- * @return
- */
- public static long parseLong(String str) {
- if (isLong(str)) {
- return Long.parseLong(str);
- }
- return 0;
- }
- /**
- * 判断一个字符串 能否转成一个int类型的
- *
- * @param str
- * @return
- */
- public static boolean isInt(String str) {
- try {
- Integer.parseInt(str);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- /**
- * 字符串转成一个Int类型的
- *
- * @param str
- * @return
- */
- public static int parseInt(String str) {
- if (isInt(str)) {
- return Integer.parseInt(str);
- }
- return 0;
- }
- /**
- * 判断一个字符串 能否转成一个float类型的
- *
- * @param str
- * @return
- */
- public static boolean isFloat(String str) {
- try {
- Float.parseFloat(str);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- /**
- * 字符串转成一个float类型的
- *
- * @param str
- * @return
- */
- public static float parseFloat(String str) {
- if (isFloat(str)) {
- return Float.parseFloat(str);
- }
- return 0f;
- }
- /**
- * 判断一个字符串 能否转成一个Double类型的
- *
- * @param str
- * @return
- */
- public static boolean checkDouble(String str) {
- Pattern pattern = Pattern.compile("^[-//+]?//d+(//.//d*)?|//.//d+$");
- return pattern.matcher(str).matches();
- }
- /**
- * String字符串的匹配(做模糊查询)
- *
- * @param objStr
- * @param inquiryStr
- * @return boolean
- */
- public static boolean vagueInquiry(String objStr, String inquiryStr) {
- if (null == objStr || null == inquiryStr) {
- return false;
- }
- Pattern p = Pattern.compile(inquiryStr);
- Matcher m = p.matcher(objStr);
- return m.find();
- }
- /**
- * 是否是汉字
- */
- public static boolean isChinese(String str) {
- String regEx = "[\u4e00-\u9fa5]";
- Pattern pat = Pattern.compile(regEx);
- Matcher matcher = pat.matcher(str);
- boolean flg = false;
- if (matcher.find())
- flg = true;
- return flg;
- }
- public boolean judgeContainsStr(String cardNum) {
- String regex = ".*[*]+.*";
- Matcher m = Pattern.compile(regex).matcher(cardNum);
- return m.matches();
- }
- /**
- * 是否为车牌号
- */
- public static boolean isCar(String carNum) {
- String regex = "^[京,津,渝,沪,冀,晋,辽,吉,黑,苏,浙,皖,闽,赣,鲁,豫,鄂,湘,粤,琼,川,贵,云,陕,秦,甘,陇,青,台,蒙,桂,宁,新,藏,澳,港,军,海,航,警,使,古][A-Z][0-9,A-Z]{5}$";
- Matcher m = Pattern.compile(regex).matcher(carNum);
- return m.matches();
- }
- /**
- * unicode码转成汉字
- */
- public static String revert(String str) {
- str = (str == null ? "" : str);
- if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回
- return str;
- StringBuffer sb = new StringBuffer(1000);
- for (int i = 0; i < str.length() - 6; ) {
- String strTemp = str.substring(i, i + 6);
- String value = strTemp.substring(2);
- int c = 0;
- for (int j = 0; j < value.length(); j++) {
- char tempChar = value.charAt(j);
- int t = 0;
- switch (tempChar) {
- case 'a':
- t = 10;
- break;
- case 'b':
- t = 11;
- break;
- case 'c':
- t = 12;
- break;
- case 'd':
- t = 13;
- break;
- case 'e':
- t = 14;
- break;
- case 'f':
- t = 15;
- break;
- default:
- t = tempChar - 48;
- break;
- }
- c += t * ((int) Math.pow(16, (value.length() - j - 1)));
- }
- sb.append((char) c);
- i = i + 6;
- }
- return sb.toString();
- }
- }
|