Android 字符串转换集合

import android.text.TextUtils;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class StringUtils {
    /**
     * 数组转字符串
     *
     * @param arr
     * @param joiner
     * @return
     */
    public static String join(String[] arr, String joiner) {
        if (arr == null || joiner == null) {
            return "";
        }
        if (arr.length == 1) {
            return arr[0];
        }
        return TextUtils.join(joiner, arr);
    }

    /**
     * 数组增加项
     * @param arr 需要追加长度的数据
     * @param addItem 追加的值
     **/
    public static String[] insert(String[] arr, String addItem) {
        String[] arr1 = new String[arr.length + 1];
        for(int j = 0;j<arr.length;j++){
            arr1[j] = arr[j];
        }
        arr1[arr.length] = addItem;
        return arr1;
    }

    /**
     * 集合转字符串
     *
     * @param arr
     * @param joiner
     * @return
     */
    public static String join(List<String> arr, String joiner) {
        if (arr == null || joiner == null) {
            return "";
        }
        if (arr.size() == 1) {
            return arr.get(0);
        }
        return TextUtils.join(joiner, arr);
    }

    /**
     * 字符串转数组
     *
     * @param source
     * @param spliter
     * @return
     */
    public static String[] split(String source, String spliter) {
        if (source == null || spliter == null) {
            return null;
        }
        if (source.endsWith(spliter)) {
            source = source.substring(0, source.length() - spliter.length());
        }
        return source.split(spliter);
    }

    /**
     * 去除source的tail
     *
     * @param source
     * @param tail
     * @return
     */
    public static String removeTail(String source, String tail) {
        if (source == null || tail == null) {
            return "";
        }
        if (source.endsWith(tail)) {
            source = source.substring(0, source.length() - tail.length());
        }
        return source;
    }

    /**
     * 去除source的head
     *
     * @param source
     * @param head
     * @return
     */
    public static String removeHead(String source, String head) {
        if (source == null || head == null) {
            return "";
        }
        if (source.startsWith(head)) {
            source = source.substring(head.length());
        }
        return source;
    }

    /**
     * 去除source的头尾
     *
     * @param source
     * @param ht
     * @return
     */
    public static String removeHT(String source, String ht) {
        if (source == null || ht == null) {
            return "";
        }
        source = removeHead(source, ht);
        source = removeTail(source, ht);
        return source;
    }


    public static String replaceMultipleIds(String source, String replaceSt, String append) {
        if (source.contains(append + replaceSt)) {
            source = source.replace(source, append + replaceSt);
        } else if (source.contains(replaceSt + append)) {
            source = source.replace(source, replaceSt + append);
        } else {
            source = source.replace(source, replaceSt);
        }
        return source;
    }

    public static String getResourceString(int stringId) {
        return BaseApplication.getContext().getResources().getString(stringId);
    }

    public static String getResourceString(int stringId, Object... params) {
        return BaseApplication.getContext().getResources().getString(stringId, params);
    }

    public static String[] getResourceStringArray(int arrayid) {
        return BaseApplication.getContext().getResources().getStringArray(arrayid);
    }


    public static int parseInt(String valueStr) {
        try {
            return Integer.parseInt(valueStr);
        } catch (Exception e) {
            return 0;
        }
    }

    public static float parseFloat(String valueStr) {
        try {
            return Float.parseFloat(valueStr);
        } catch (Exception e) {
            return 0.00f;
        }
    }

    public static double parseDouble(String valueStr) {
        try {
            return Double.parseDouble(valueStr);
        } catch (Exception e) {
            return 0.00d;
        }
    }

    /**
     * 拼接一维数组
     *
     * @param first
     * @param data
     * @param regex
     * @return
     */
    public static String[] spliceRegex(String[] first, String data, String regex) {
        String[] strs = data.split(regex);
        if (first != null) {
            first = Arrays.copyOf(first, first.length + strs.length);
            System.arraycopy(strs, 0, first, first.length - strs.length, strs.length);
        }
        return first;
    }

    /**
     * 二维数组
     *
     * @param first
     * @param data
     * @param regex0
     * @param regex1
     * @return
     */
    public static String[][] spliceRegex(String[] first, String data, String regex0, String regex1) {
        if (TextUtils.isEmpty(data)) {
            return new String[0][0];
        }

        String[] infos0 = data.split(regex0);
        String[][] infos1 = null;
        for (int i = 0; i < infos0.length; i++) {

            String[] info = spliceRegex(first, infos0[i], regex1);

            if (infos1 == null) {
                infos1 = new String[infos0.length][info.length];
            }
            infos1[i] = info;
        }
        return infos1;
    }

    /**
     * 中文关键字正则匹配
     *
     * @param str
     * @param rexgStr
     * @return
     */
    public static boolean isMatcher(String str, String rexgStr) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(rexgStr)) {
            return false;
        }

        return str.matches(rexgStr);
    }

    /**
     * 获取中文正则表达式
     *
     * @param rexgStr
     * @return
     */
    public static String getRexgStr(String rexgStr) {
        StringBuilder rexg = new StringBuilder("(.*)");
        for (int i = 0; i < rexgStr.length(); i++) {
            rexg.append(rexgStr.charAt(i)).append("(.*)");
        }

        return rexg.toString();
    }


    public static boolean isEmptyRecordId(String recordId) {
        return TextUtils.isEmpty(recordId) || "00000000-0000-0000-0000-000000000000".equals(recordId);
    }

    public static int getStringContainsCount(String str, String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key, index)) != -1) {
            index = index + key.length();
            count++;
        }
        return count;
    }

    public static List<String> getStringContainsCountSplitTitles(String str, String key) {
        List<String> titles = new ArrayList<>();
        int mIndex = 0;//初始化 当前匹配到字符串的开始index
        int titleStartIndex = 0;//标题开始index
        while ((mIndex = str.indexOf(key, titleStartIndex)) != -1) {
            if (mIndex == 0 && titleStartIndex == mIndex) {
                titles.add("");
            } else if (titleStartIndex == mIndex) {
                titles.add("");
            } else {
                titles.add(str.substring(titleStartIndex, mIndex));
            }
            titleStartIndex = mIndex + key.length();
        }

        if (str.length() - 1 >= titleStartIndex) {
            titles.add(str.substring(titleStartIndex));
        } else {
            titles.add("");
        }
        return titles;
    }

    /**
     * 数字输入时格式化
     * <p>dotAfterLength小于1时,保留小数点后4位;dotAfterLength等于Integer.MAX_VALUE时不限制小数位数</p>
     *
     * @param before         对应TextWatcher_beforeTextChanged文本
     * @param after          对应TextWatcher_afterTextChanged文本
     * @param dotAfterLength 保留小数点后dotAfterLength位
     * @return
     */
    public static String ruleNumberAfterTextChanged(String before, String after, int dotAfterLength) {
        String ruleStr = after;
        if (dotAfterLength < 1) {//默认保留小数点后4位
            dotAfterLength = 4;
        }
        if (dotAfterLength != Integer.MAX_VALUE) {
            dotAfterLength = Math.min(dotAfterLength, 10);//最大支持小数点后10位

            //保留小数点后dotAfterLength位
            if (dotAfterLength > 0 && after.contains(".") && after.substring(after.indexOf(".")).length() > dotAfterLength) {
                ruleStr = after.substring(0, after.indexOf(".") + 1 + dotAfterLength);
            }
        }

        //如果第一位是0
        //再输入0的话,不能输入
        //再输入>0的数,去掉前面的0
        if (after.startsWith("0") && after.length() > 1) {
            if (after.startsWith("0", 1)) {
                ruleStr = before;
            } else if (!after.startsWith(".", 1)) {
                ruleStr = after.substring(1);
            }
        }

        return ruleStr;
    }

    /**
     * 数字结果格式化
     *
     * @param number
     * @return
     */
    public static String ruleNumber(String number) {
        String numberStr = "";
        try {
            double numberDouble = Double.valueOf(number);

            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(10);//最大支持小数点后10位
            nf.setGroupingUsed(false);//去掉千分位
            numberStr = nf.format(numberDouble);

            if ("-0".equals(numberStr)) {
                numberStr = "0";
            }
        } catch (Exception e) {
        }
        return numberStr;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值