随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

string.format()详解

1.String类:String.format()

这货有2种重载形式:

    format(String format, Object… args)
        新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。
    format(Locale locale, String format, Object… args)
        使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。
转换符详细说明示例
%s 字符串类型 “喜欢请收藏”
%c 字符类型 ‘m’
%b 布尔类型 true
%d 整数类型(十进制) 88
%x 整数类型(十六进制) FF
%o 整数类型(八进制) 77
%f 浮点类型 8.888
%a 十六进制浮点类型 FF.35AE
%e 指数类型 9.38e+5
%g 通用浮点类型(f和e类型中较短的) 不举例(基本用不到)
%h 散列码 不举例(基本用不到)
%% 百分比类型 %(%特殊字符%%才能显示%)
%n 换行符 不举例(基本用不到)
%tx 日期与时间类型(x代表不同的日期与时间转换符) 不举例(基本用不到)
StringUtils.chomp()

chomp剔除字符串最后一个是\n或\r\或\r\n源码详解

复制代码
        public static String chomp(String str)
        {
          if (isEmpty(str)) {
            return str;
          }

          if (str.length() == 1) {
            char ch = str.charAt(0);
            if ((ch == '\r') || (ch == '\n')) {
              return "";
            }
            return str;
          }

          int lastIdx = str.length() - 1;
          char last = str.charAt(lastIdx);

          if (last == '\n') {
            if (str.charAt(lastIdx - 1) == '\r') {
              lastIdx--;
            }
          } else if (last != '\r') {
            lastIdx++;
          }
          return str.substring(0, lastIdx);
        }
复制代码

如上代码可以分析chomp方法将会剔除掉字符串末的\n、\r、\r\n(换行、回车、回车换行符)

复制代码
 StringUtils.chomp(null)          = null
 StringUtils.chomp("")            = ""
 StringUtils.chomp("abc \r")      = "abc "
 StringUtils.chomp("abc\n")       = "abc"
 StringUtils.chomp("abc\r\n")     = "abc"
 StringUtils.chomp("abc\r\n\r\n") = "abc\r\n"
 StringUtils.chomp("abc\n\r")     = "abc\n"
 StringUtils.chomp("abc\n\rabc")  = "abc\n\rabc"
 StringUtils.chomp("\r")          = ""
 StringUtils.chomp("\n")          = ""
 StringUtils.chomp("\r\n")        = ""
复制代码

 

posted on   小破孩楼主  阅读(513)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
历史上的今天:
2021-04-25 idea查看源代码出现/* compiled code */
2021-04-25 request.getRequestURL()和request.getRequestURI()的区别
2021-04-25 如何判断指定Class是否是基础数据类型或者是其包装类型
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示