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;
        }
    }

  • 相关阅读:
    Thinkphp学习笔记2-
    Thinkphp学习笔记1-URL模式
    WebApi-如何实现接口加密
    微信-.NET调用JS-SDK
    微信-JSSDK .NET版
    HTML-获取/修改html页面标题
    JS-获取图片地址
    微信-js sdk invalid signature签名错误 问题解决
    C++笔试题
    单链表反转的分析及实现
  • 原文地址:https://www.cnblogs.com/Snowflake/p/3412216.html
Copyright © 2011-2022 走看看