com.fjn.helper.common.util.StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.fjn.helper.common.util.StringUtil.java

Source

/*
 *
 *  Copyright 2018 FJN Corp.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  Author                        Date                       Issue
 *  fs1194361820@163.com          2015-01-01                 Initial Version
 *
 */

package com.fjn.helper.common.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings({ "unchecked", "rawtypes" })
/**
 *
 * @author fs1194361820@163.com
 *
 */
public class StringUtil extends StringUtils {
    private static final Logger log = LoggerFactory.getLogger(StringUtil.class);

    public static boolean isOk(String str) {
        return str != null && !str.isEmpty() && !str.trim().isEmpty();
    }

    public static boolean isNull(String str) {
        if (str != null && str.length() != 0) {
            return false;
        }
        return true;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean isBlank(String str) {
        return str == null || str.isEmpty() || str.trim().isEmpty();
    }

    public static String toGBK(String str) throws UnsupportedEncodingException {
        return EncodingUtil.toEncoding(str, "ISO-8859-1", "GBK");
    }

    /**
     * strISO-8859-1???UTF-8?
     * @see {@link EncodingUtil#toEncoding(String, String, String)}
     * @param str ??
     * @return ??
     */
    public static String toUTF8(String str) {
        return EncodingUtil.toEncoding(str, EncodingUtil.ISO88591, EncodingUtil.UTF8);
    }

    /**
     *  int  String ? 0 ?
     */
    public static String tofixLengthString(int val, int scale) {
        String sVal = Integer.toString(val);
        int len = scale - sVal.length();

        if (sVal.length() < scale)
            for (int i = 0; i < len; i++)
                sVal = "0" + sVal;

        return sVal;
    }

    /**
     * Listsplit
     * @param list
     * @param split
     * @return
     */
    public static String collectionToString(Collection list, String split) {
        if (isEmpty(split)) {
            split = ", ";
        }
        StringBuilder buf = new StringBuilder();
        if (list != null && !list.isEmpty()) {
            Iterator iter = list.iterator();
            if (iter.hasNext()) {
                buf.append(iter.next());
            }
            while (iter.hasNext()) {
                buf.append(split).append(iter.next());
            }
        }
        return buf.toString();
    }

    public static String arrayToString(Object[] arr, String split) {
        if (isEmpty(split)) {
            split = ", ";
        }
        StringBuilder buf = new StringBuilder();
        if (arr != null && arr.length > 0) {
            buf.append(arr[0]);
            for (int i = 1; i < arr.length; i++) {
                buf.append(split).append(arr[i]);
            }
        }
        return buf.toString();
    }

    /**
     * split????
     * @param list
     * @param split
     * @return
     */
    public static List<String> toList(String sList, String split) {
        return Arrays.asList(sList.split(split));
    }

    /**
     * dateFromDB??,?yyyy-MM-dd hh:mm:ss.n ???yyyy-MM-dd
     * @param dateFromDB
     * @return
     */
    public static String getYMonth(String dateFromDB) {
        return dateFromDB.subSequence(0, 10).toString();
    }

    /**
     *  {-?}??
     *  ?????0???-1
     */
    public static int binToDec(String str) {
        int result = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != '0' && str.charAt(i) != '1') {
                return -1;
            }
            result = result * 2 + str.charAt(i) - '0';
        }
        return result;
    }

    /**
     * 10?2?8?16
     * @param num
     * @return
     */
    public static String dec2Bin_Oct_Hex(int num, int flag) {
        if (flag == 2) {
            return Integer.toBinaryString(num);
        } else if (flag == 8) {
            return Integer.toOctalString(num);
        } else if (flag == 16) {
            return Integer.toHexString(num);
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        List list = new ArrayList();
        for (int i = 0; i < 10; i++)
            list.add("test");
        String src = StringUtil.collectionToString(list, ", ");
        log.info(src);
        for (Object object : StringUtil.toList(src, ", ")) {
            log.info(object.toString());
        }
        log.info(StringUtil.isNull("") + "");
        log.info(StringUtil.isNull("  ") + "");
        log.info(StringUtil.isNull(null) + "");
        log.info(StringUtil.isNull("\n") + "");
    }
}