zoukankan      html  css  js  c++  java
  • Java常用工具类

    import java.io.UnsupportedEncodingException;
    
    /**
     * 字符串工具类
     * 
     * @author 尘世间迷茫的小书童
     *
     */
    public class StringUtils {
    
        /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */
        public static final String US_ASCII = "US-ASCII";
    
        /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */
        public static final String ISO_8859_1 = "ISO-8859-1";
    
        /** 8 位 UCS 转换格式 */
        public static final String UTF_8 = "UTF-8";
    
        /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */
        public static final String UTF_16BE = "UTF-16BE";
    
        /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */
        public static final String UTF_16LE = "UTF-16LE";
    
        /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */
        public static final String UTF_16 = "UTF-16";
    
        /** 中文超大字符集 */
        public static final String GBK = "GBK";
    
        /**
         * 判断字符串是否为空
         * 
         * @param str
         * @return 为空返回true
         */
        public static boolean isEmpty(String str) {
            return null == str && str.length() == 0;
        }
    
        /**
         * 判断字符串是否不为空
         * 
         * @param str
         * @return 不为空返回true
         */
        public static boolean isNotEmpty(String str) {
            return null != str && str.length() > 0;
        }
    
        /**
         * 去掉字符串两头的空字符,str为空返回null
         * 
         * @param str
         * @return
         */
        public static String trim(String str) {
            if (isEmpty(str))
                return null;
            return str.trim();
        }
    
        /**
         * 替换字符串
         * 
         * @param str
         * @param oldChar
         * @param newChar
         * @return
         */
        public static String replace(String str, String oldChar, String newChar) {
            return str.replace(oldChar, newChar);
        }
    
        /**
         * 可以替换大部分空白字符, 不限于空格
         * 
         * @param str
         * @return
         */
        public static String replace(String str) {
            return str.replaceAll("\s*", "");
        }
    
        /**
         * 根据start和end范围用newStr覆盖str
         * 
         * @param str
         * @param newStr
         * @param start
         * @param end
         * @return
         */
        public static String overlay(String str, String newStr, int start, int end) {
            char[] charArray = str.toCharArray();
            char[] startArray = new char[start];
            int length = charArray.length - end - 1;
            char[] endArray = new char[length];
            for (int i = 0; i < start; i++) {
                startArray[i] = charArray[i];
            }
            for (int i = 0; i < length; i++) {
                end++;
                endArray[i] = charArray[end];
            }
            return String.valueOf(startArray) + newStr + String.valueOf(endArray);
        }
    
        /**
         * 如果str中是以start开头,则删除开头,否者返回原字符串
         * 
         * @param str
         * @param start
         * @return
         */
        public static String removeStart(String str, String start) {
            int startIndex = str.indexOf(start);
            if (startIndex == 0)
                return overlay(str, "", 0, startIndex + 1);
            return str;
        }
    
        /**
         * 如果str中是以end结尾,则删除结尾,否者返回原字符串
         * 
         * @param str
         * @param end
         * @return
         */
        public static String removeEnd(String str, String end) {
            int endIndex = str.lastIndexOf(end);
            if (endIndex == str.length() - end.length())
                return overlay(str, "", endIndex, str.length() - 1);
            return str;
        }
    
        /**
         * 删除str中所有空白字符
         * 
         * @param str
         * @return
         */
        public static String deleteWhitespace(String str) {
            return str.replace(" ", "");
        }
    
        /**
         * 计算str1在str中出现次数
         * 
         * @param str
         * @param str1
         * @return
         */
        public static int countStr(String str, String str1) {
            if (isEmpty(str) || isEmpty(str1))
                return 0;
            int count = 0;
            int position = 0;
            int index;
            while ((index = str.indexOf(str1, position)) != -1) {
                ++count;
                position = index + str1.length();
            }
            return count;
        }
    
        /**
         * 反转字符串
         * 
         * @param str
         * @return
         */
        public static String reverseStr1(String str) {
            return new StringBuilder(str).reverse().toString();
        }
    
        /**
         * 反转字符串
         * 
         * @param str
         * @return
         */
        public static String reverseStr2(String str) {
            char[] chars = str.toCharArray();
            String reverse = "";
            for (int i = chars.length - 1; i >= 0; i--) {
                reverse += chars[i];
            }
            return reverse;
        }
    
        /**
         * 反转字符串
         * 
         * @param str
         * @return
         */
        public static String reverseStr3(String str) {
            String reverse = "";
            int length = str.length();
            for (int i = 0; i < length; i++) {
                reverse = str.charAt(i) + reverse;
            }
            return reverse;
        }
    
        /**
         * 根据sub拆分str为数组,使用. |分割时需要加\
         * 
         * @param str
         * @param sub
         * @return
         */
        public static String[] split(String str, String sub) {
            return str.split(sub);
        }
    
        /**
         * 字符转String
         * 
         * @param c
         * @return
         */
        public static String charToString(char c) {
            return String.valueOf(c);
        }
    
        /**
         * 字符数组转String
         * 
         * @param c
         * @return
         */
        public static String charArrayToString(char[] c) {
            return String.valueOf(c);
        }
    
        /**
         * 字符数组转String
         * 
         * @param c
         * @param start
         * @param end
         * @return
         */
        public static String charArrayToString(char[] c, int start, int end) {
            return String.valueOf(c, start, end);
        }
    
        /**
         * String数组转String
         * 
         * @param strs
         * @return
         */
        public static String strArrayToStr(String[] strs) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < strs.length; i++) {
                buffer.append(strs[i]);
            }
            return buffer.toString();
        }
    
        /**
         * 转换字符串编码格式
         * 
         * @param str
         * @param oldCharset
         * @param newCharset
         * @return
         * @throws UnsupportedEncodingException
         */
        public static String changeCharset(String str, String oldCharset, String newCharset)
                throws UnsupportedEncodingException {
            return new String(str.getBytes(oldCharset), newCharset);
        }
    
        /**
         * 转换字符串编码格式
         * 
         * @param str
         * @param newCharset
         * @return
         * @throws UnsupportedEncodingException
         */
        public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
            return new String(str.getBytes(), newCharset);
        }
    
        /**
         * 将字符编码转换成US-ASCII码
         */
        public String toASCII(String str) throws UnsupportedEncodingException {
            return changeCharset(str, US_ASCII);
        }
    
        /**
         * 将字符编码转换成ISO-8859-1码
         */
        public String toISO_8859_1(String str) throws UnsupportedEncodingException {
            return changeCharset(str, ISO_8859_1);
        }
    
        /**
         * 将字符编码转换成UTF-8码
         */
        public String toUTF_8(String str) throws UnsupportedEncodingException {
            return changeCharset(str, UTF_8);
        }
    
        /**
         * 将字符编码转换成UTF-16BE码
         */
        public String toUTF_16BE(String str) throws UnsupportedEncodingException {
            return changeCharset(str, UTF_16BE);
        }
    
        /**
         * 将字符编码转换成UTF-16LE码
         */
        public String toUTF_16LE(String str) throws UnsupportedEncodingException {
            return changeCharset(str, UTF_16LE);
        }
    
        /**
         * 将字符编码转换成UTF-16码
         */
        public String toUTF_16(String str) throws UnsupportedEncodingException {
            return changeCharset(str, UTF_16);
        }
    
        /**
         * 将字符编码转换成GBK码
         */
        public String toGBK(String str) throws UnsupportedEncodingException {
            return changeCharset(str, GBK);
        }
    
        public static void main(String[] args) {
    
            // String replace = replace("maxinhai", "ma", "li");
            // System.out.println(replace);
    
            // String overlay = overlay("maxinhai", "li", 0, 4);
            // System.out.println(overlay);
    
            // String removeSatrt = removeStart("maxinhai", "ma");
            // System.out.println(removeSatrt);
    
            // String removeEnd = removeEnd("maxinhai", "hai");
            // System.out.println(removeEnd);
    
            // String deleteWhitespace = deleteWhitespace(" ma xin hai ");
            // System.out.println(deleteWhitespace);
    
            // String replace = replace(" ma xin hai	 ");
            // System.out.println(replace);
    
            // int countStr = countStr("121121212121211212", "1");
            // System.out.println(countStr);
    
        }
    
    }

    各种个工具类。。。

    总结以后,方便copy

  • 相关阅读:
    Debugging Kafka connect
    android O 蓝牙设备默认名称更改
    qualcomm sdm450 tinymix mic record
    QACT 在线调试 Android O
    Android O seLinux 编译错误
    Android seLinux 设置
    高通 fastboot 显示
    高通 双MIC 设置
    高通 添加 cmdline
    高通 mixer_paths.xml 音频配置文件 初始化过程
  • 原文地址:https://www.cnblogs.com/mxh-java/p/11395749.html
Copyright © 2011-2022 走看看