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

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

Description

format

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 (isEmpty(str))
            return "";
        if (args.length == 0)
            return str;
        String result = str;/*  www . j ava 2 s . c  o  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 && args[index] != null) {
                result = result.replace(m.group(), args[index].toString());
            }
        }
        return result;
    }

    public static boolean isEmpty(String value) {
        return value == null || value.trim().equals("")
                || value.length() == 0;
    }
}

Related

  1. format(String str, String obj)
  2. format(String str, String[] obj)
  3. format(String str, Object... obj)
  4. simpleFormat(String s, Object aobj[])
  5. format(String str, Object... args)
  6. formatAccountNo(String accountNo)
  7. formattedNumber(String number)