zoukankan      html  css  js  c++  java
  • java 字符串截取

    截取指定长度的字符串,如果超出就用more的内容来替换
     截取的字节数,截取的时候,有可能会少截取一位(当最后一位是1个双字节的话,会少截取一个)

    public class Test {
        public static void main(String[] args) {
            String s="a测试bcd试1";
            System.err.println(subAndReplaceString(s, 50, "..."));
        }
        
        public static String subAndReplaceString(String str, int toCount, String more) {
            int reInt = 0;
            String reStr = "";
            if (str == null)
                return "";
            char[] tempChar = str.toCharArray();
            for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {
                String s1 = String.valueOf(tempChar[kk]);
                byte[] b = s1.getBytes();
                reInt += b.length;
                if (reInt > toCount)
                    break;
                reStr += tempChar[kk];
            }
            if (toCount == reInt || (toCount == reInt - 1))
                reStr += more;
            return reStr;
        }
    }

  • 相关阅读:
    oracle执行.sql文件
    rematch的基本用法
    dva的基本用法
    redux-saga基本用法
    react-redux的基本用法
    redux的基本概念
    mobx基本概念
    centos 编译安装Apache 2.4
    javascript动态添加一组input
    php配置文件语法
  • 原文地址:https://www.cnblogs.com/Snowflake/p/3412216.html
Copyright © 2011-2022 走看看