String Formatter in GWT

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

    A very simple replacement for String.format() in GWT 2.1+:

    import com.google.gwt.regexp.shared.RegExp;
    import com.google.gwt.regexp.shared.SplitResult;
    
    public static String format(final String format, final Object... args) {
      final RegExp regex = RegExp.compile("%[a-z]");
      final SplitResult split = regex.split(format);
      final StringBuffer msg = new StringBuffer();
      for (int pos = 0; pos < split.length() - 1; ++pos) {
        msg.append(split.get(pos));
        msg.append(args[pos].toString());
      }
      msg.append(split.get(split.length() - 1));
      return msg.toString();
    }
    

提交回复
热议问题