String Formatter in GWT

前端 未结 12 650
生来不讨喜
生来不讨喜 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:56

    See the official page on GWT date and number formatting.

    They suggest the following:

    myNum decimal = 33.23232;
    myString = NumberFormat.getFormat("#.00").format(decimal);
    

    It is best to use their supported, optimized methods, than to cook up your own non-optimal method. Their compiler will be optimizing them all to nearly the same thing anyway in the end.

    0 讨论(0)
  • 2021-02-03 19:56

    Another suggestion which makes use of JSNI and a nice JavaScript format function from another post:

    import com.google.gwt.core.client.JsArrayString;
    
    public abstract class StringFormatter {
        public static String format(final String format, final Object... args) {
            if (null == args || 0 == args.length)
                return format;
            JsArrayString array = newArray();
            for (Object arg : args) {
                array.push(String.valueOf(arg)); // TODO: smarter conversion?
            }
            return nativeFormat(format, array);
        }
    
        private static native JsArrayString newArray()/*-{
            return [];
        }-*/;
    
        private static native String nativeFormat(final String format, final JsArrayString args)/*-{
            return format.replace(/{(\d+)}/g, function(match, number) {
                return typeof args[number] != 'undefined' ? args[number] : match;
            });
        }-*/;
    }
    

    One can then make a call like this:

    StringFormatter.format("Greetings {0}, it's {1} o'clock, which is a {2} statement", "Master", 8, false);
    

    ...with the result being

    Greetings Master, it's 8 o'clock, which is a false statement

    There is a potential to improve further at the TODO comment, e.g. utilise NumberFormat. Suggestions are welcome.

    0 讨论(0)
  • 2021-02-03 19:57

    As an alternative you can use class NumberFormat:

    NumberFormat fmt = NumberFormat.getDecimalFormat();
    double value = 12345.6789;
    String formatted = fmt.format(value);
    // Prints 1,2345.6789 in the default locale
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-02-03 20:00

    UPDATE: Please see (and vote up) Joseph Lust's post below before looking further at this answer.

    Looks like formatter isn't included according to this post. However, they suggest some alternatives.

    0 讨论(0)
  • 2021-02-03 20:00

    Maybe the easiest way to do something like String.format, can be do it with a String.replace, for instance;

    instead of do String.format("Hello %s", "Daniel"); ==> "Hello %s".replace("%s", "Daniel"),

    both give us the same result, but just the second way works in GWT client side

    0 讨论(0)
提交回复
热议问题