String Formatter in GWT

前端 未结 12 679
生来不讨喜
生来不讨喜 2021-02-03 18:59

How do I format my string in GWT?

I made a method

  Formatter format = new Formatter();
    int matches = 0;
    Formatter formattedString = format.forma         


        
12条回答
  •  暖寄归人
    2021-02-03 19:58

    another very very simple replacement for java.text.MessageFormat.format() :

    public static String format(final String format, final Object... args) {
        StringBuilder sb = new StringBuilder();
        int cur = 0;
        int len = format.length();
        while (cur < len) {
            int fi = format.indexOf('{', cur);
            if (fi != -1) {
                sb.append(format.substring(cur, fi));
                int si = format.indexOf('}', fi);
                if (si != -1) {
                    String nStr = format.substring(fi + 1, si);
                    int i = Integer.parseInt(nStr);
                    sb.append(args[i]);
                    cur = si + 1;
                } else {
                    sb.append(format.substring(fi));
                    break;
                }
            } else {
                sb.append(format.substring(cur, len));
                break;
            }
        }
        return sb.toString();
    }
    

提交回复
热议问题