All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.StringUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import matrix.boot.common.exception.ServiceException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 字符串工具
 *
 * @author wangcheng
 */
public class StringUtil {

    /**
     * 空字符串
     */
    public static final String EMPTY_STRING = "";

    /**
     * 编码字符串
     *
     * @param content 内容
     * @return 编码后的字符串
     */
    public static String encodeStr(String content) {
        return new String(content.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
    }

    /**
     * 解码字符串
     *
     * @param content 内容
     * @return 解码后的字符串
     */
    public static String decodeStr(String content) {
        return new String(content.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    }

    /**
     * 字节转字符串
     *
     * @param bytes 字节
     * @return 字符串
     */
    public static String byteToString(byte[] bytes) {
        return new String(bytes, StandardCharsets.UTF_8);
    }

    /**
     * 字符串转字节
     *
     * @param content 内容
     * @return 字节
     */
    public static byte[] stringToByte(String content) {
        return content.getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 编码URL字符串
     *
     * @param content 内容
     * @return 编码后的URL字符串
     */
    public static String encodeUrl(String content) {
        try {
            return URLEncoder.encode(content, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 解码URL字符串
     *
     * @param content 内容
     * @return 解码后的URL字符串
     */
    public static String decodeUrl(String content) {
        try {
            return URLDecoder.decode(content, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 判断字符串是否为空
     *
     * @param content 内容
     * @return 是否
     */
    public static boolean isEmpty(String content) {
        return content == null || "".equals(content);
    }

    /**
     * 判断字符串不为空
     *
     * @param content 内容
     * @return 是否
     */
    public static boolean notEmpty(String content) {
        return !isEmpty(content);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy