StringUtils.java 8.08 KB
Newer Older
1
package com.ydl.ydlcommon.utils;
konghaorui committed
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275


import android.text.TextUtils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.PatternSyntaxException;

/**
 * 字符串转换工具类
 * Created by xiongyu on 2017/3/17.
 */

public class StringUtils {

    /**
     * 处理文字中未显示的自定义表情
     *
     * @param orgin_str
     * @return
     */
    public static String CustomEmojiSub(String orgin_str) {
        int pre_index = orgin_str.lastIndexOf("[");
        if (pre_index != -1) {
            if (orgin_str.substring(pre_index).indexOf("]") != -1) {//表示包含了最后一个自定义表情
                return orgin_str;
            } else {
                return orgin_str.substring(0, pre_index) + "...";
            }
        } else {
            return orgin_str;
        }
    }


    public static String UrlToString(String url_str) {
        String deal_str = "";
        String type_str = url_str.substring(url_str.lastIndexOf("."));
        deal_str = url_str.replace("http", "ydl");
        deal_str = deal_str.replace(":", "a");
        deal_str = deal_str.replace("/", "a");
        deal_str = deal_str.replace(".", "a");
        deal_str = deal_str + "." + type_str;
        return deal_str;
    }

    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断该字符串是否为中文
     *
     * @param string
     * @return
     */
    public static boolean isChinese(String string) {
        int n = 0;
        for (int i = 0; i < string.length(); i++) {
            n = (int) string.charAt(i);
            if (!(19968 <= n && n < 40869)) {
                return false;
            }
        }
        return true;
    }

    /**
     * counter ASCII character as one, otherwise two
     *
     * @param str
     * @return count
     */
    public static int counterChars(String str) {
        // return
        if (TextUtils.isEmpty(str)) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            int tmp = (int) str.charAt(i);
            if (tmp > 0 && tmp < 127) {
                count += 1;
            } else {
                count += 2;
            }
        }
        return count;
    }

    /**
     * 获取32位uuid
     *
     * @return
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String get36UUID() {
        UUID uuid = UUID.randomUUID();
        String uniqueId = uuid.toString();
        return uniqueId;
    }


    public static String StringFilter(String str) throws PatternSyntaxException {
        String result = "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (StringUtils.isEmojiCharacter(str.charAt(i))) {
                sb.append(str.charAt(i));
            }
        }
        result = sb.toString();
        return result;
    }

    public static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }

    /***
     * 判断跳转H5还是打开原生页面
     * @param A
     * @return
     */
    public static Map<String, String> IsHttpReturn(String A) {
        Map<String, String> map = new HashMap<>();
        if (A.contains("http://") || A.contains("https://")) { //表明为跳转url
            map.put("jump_type", "url");
            if (A.contains("experts")) {//判断跳转那种类型的H5页面
                if (A.contains("search")) {
                    map.put("url_page", "experts_search");//专家搜索
                } else {
                    map.put("url_page", "experts");//专家主页
                }
            } else if (A.contains("ct/list")) {//免费体验
                map.put("url_page", "free_trial");
            } else if (A.contains("ceshi/detail") || A.contains("test/detail")) { //测试
                map.put("url_page", "test_detail");
            } else if (A.contains("ceshi/result") || A.contains("test/result")) { //测试
                map.put("url_page", "test_result");
            } else if (A.contains("ceshi/")) { //测试
                map.put("url_page", "test");
            } else {
                map.put("url_page", "other");//其他的
            }
            map.put("url", A);
        } else if (A.contains("ydl://")) {//表明跳转为原生
            map.put("jump_type", "native");
            String native_page = A.substring(6, A.indexOf("?"));
            map.put("native_page", native_page);
            map.putAll(TemporaryUtils.getUrlParams(A));

        } else {
            map.put("jump_type", "no");
        }

        return map;
    }

    /**
     * 对字符串进行MD5加密</br>
     * 如果返回为空,则表示加密失败
     *
     * @param s
     * @return
     */
    public static String md5(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] strTemp = s.getBytes("utf-8");
            // 使用MD5创建MessageDigest对象
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte b = md[i];
                // 将每个数(int)b进行双字节加密
                str[k++] = hexDigits[b >> 4 & 0xf];
                str[k++] = hexDigits[b & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0;
    }


    /**
     * 判断传入时间与当前时间是否是同一天
     *
     * @param last 需要被比较的时间
     * @return
     */
    public static boolean compareTime(String last) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//如2016-08-10
        if (TextUtils.isEmpty(last) || TextUtils.equals("0", last)) {
            return false;
        } else {
            long time = System.currentTimeMillis();
            Date date = new Date();
            date.setTime(time);
            String nowTime = simpleFormat.format(date);
            Date lastDate = new Date();
            lastDate.setTime(Long.valueOf(last));
            String lastTime = simpleFormat.format(lastDate);
            if (TextUtils.equals(lastTime.substring(0, 10), nowTime.substring(0, 10))) {
                //是同一天
                return true;
            } else {
                //不是同一天
                return false;
            }
        }
    }

    /**
     * 保留两位小数
     */
    public static String save2Double(double value){
        String str = "--";
        try {
            str = String.format("%.2f",value).toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 字符串以逗号分割转List
     */
    public static List<String> strToList(String str){
        List list = new ArrayList();
        if (TextUtils.isEmpty(str)){
            return list;
        }
        String[] ss = str.split("\\|");
        return Arrays.asList(ss);
    }
}