Android String Format format(String str, Object... args)

Here you can find the source of format(String str, Object... args)

Description

format

License

Open Source License

Declaration

public static String format(String str, Object... args) 

Method Source Code

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String format(String str, Object... args) {
        if (isEmptyOrNull(str))
            return "";
        if (args.length == 0)
            return str;
        String result = str;//from   w  w w . ja v a 2  s. co  m
        Pattern p = java.util.regex.Pattern.compile("\\{(\\d+)\\}");
        Matcher m = p.matcher(str);
        while (m.find()) {
            int index = Integer.parseInt(m.group(1));
            if (index < args.length) {
                result = result.replace(m.group(), args[index].toString());
            }
        }
        return result;
    }

    public static boolean isEmptyOrNull(String str) {
        return str == null || str.length() == 0
                || str.contentEquals("null") || str.trim().equals("");
    }
}

Related

  1. format(String str, String obj)
  2. format(String str, String[] obj)
  3. format(String str, Object... obj)
  4. format(String str, Object... args)
  5. simpleFormat(String s, Object aobj[])
  6. formatAccountNo(String accountNo)
  7. formattedNumber(String number)
  8. currentTime(CharSequence inFormat)
  9. formatIndent(String whiteSpace)