扯淡闲客 2022-01-08 14:58 采纳率: 50%
浏览 26

String类的方法里的Format方法如何做到格式化?如果是转义,转义后是否会变成其他数据类型?

//这个转义之后是String类型的1?还是整数类型的1?
        String str = "hi";
        str=String.format("%d", 1);

  • 写回答

1条回答 默认 最新

  • IT不码农 2022-01-08 20:50
    关注

    先回答String.format("%d", 1)是个格式化方法,它返回的就是字符串String。第一参数的含义是格式化模板,第二是可变参数,表示要格式化的参数。
    其格式化的过程:
    1、现在格式化模板中找到特殊的格式化标记,并记录下标记所在模板的位置索引,把这些标记记录到一个list中
    2、根据这个list顺序,将第二参数列表的参数进行格式化处理成想要的字符串,并替换到模板中标记的位置,拼接成最终要的字符串返回出去
    于是我就去看了下jdk里源码,其实现思路跟我分析的过程很像,就节选了部分代码

     public Formatter format(Locale l, String format, Object ... args) {
            ensureOpen();
    
            // index of last argument referenced
            int last = -1;
            // last ordinary index
            int lasto = -1;
           //此方法就过程的第一步去找特殊标记
            FormatString[] fsa = parse(format);
            for (int i = 0; i < fsa.length; i++) {
                FormatString fs = fsa[i];
                int index = fs.index();
                try {
                    switch (index) {
                    case -2:  // fixed string, "%n", or "%%"
                        fs.print(null, l);
                        break;
                    case -1:  // relative index
                        if (last < 0 || (args != null && last > args.length - 1))
                            throw new MissingFormatArgumentException(fs.toString());
                        fs.print((args == null ? null : args[last]), l);
                        break;
                    case 0:  // ordinary index
                        lasto++;
                        last = lasto;
                        if (args != null && lasto > args.length - 1)
                            throw new MissingFormatArgumentException(fs.toString());
                        fs.print((args == null ? null : args[lasto]), l);
                        break;
                    default:  // explicit index
                        last = index - 1;
                        if (args != null && last > args.length - 1)
                            throw new MissingFormatArgumentException(fs.toString());
                        fs.print((args == null ? null : args[last]), l);
                        break;
                    }
                } catch (IOException x) {
                    lastException = x;
                }
            }
            return this;
        }
    
    
        /**
         * Finds format specifiers in the format string.
         */
        private FormatString[] parse(String s) {
            ArrayList<FormatString> al = new ArrayList<>();
            Matcher m = fsPattern.matcher(s);
            for (int i = 0, len = s.length(); i < len; ) {
                if (m.find(i)) {
                    // Anything between the start of the string and the beginning
                    // of the format specifier is either fixed text or contains
                    // an invalid format string.
                    if (m.start() != i) {
                        // Make sure we didn't miss any invalid format specifiers
                        checkText(s, i, m.start());
                        // Assume previous characters were fixed text
                        al.add(new FixedString(s.substring(i, m.start())));
                    }
    
                    al.add(new FormatSpecifier(m));
                    i = m.end();
                } else {
                    // No more valid format specifiers.  Check for possible invalid
                    // format specifiers.
                    checkText(s, i, len);
                    // The rest of the string is fixed text
                    al.add(new FixedString(s.substring(i)));
                    break;
                }
            }
            return al.toArray(new FormatString[al.size()]);
        }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月8日

悬赏问题

  • ¥15 压力传感器控制led
  • ¥15 matlab使用报错
  • ¥15 如何用BL55072驱动屏幕
  • ¥100 网页利用NC文件数据生成云图
  • ¥15 vivo手机的101root检测通常包括Bootloader状态检测吗,有没有懂的人来回答下?
  • ¥15 c++ 单例模式 在不项目之间的引用问题
  • ¥15 我想在微信名中用上红桃♥️这个表情 可是保存不了 说是有特殊符号或者不支持的语言 需要怎么做呢
  • ¥15 oracle报skip not validate connection
  • ¥15 请问左奇异向量怎么求解呢?
  • ¥15 Arcgis pro制图