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

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

Description

format

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/**//from ww w .j  ava  2s  .  com
 * Copyright 2013 ??????
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final String EMPTY = "";

    public static String format(String str, String obj) {
        if (str == null || obj == null)
            return str;

        return replace(str, "{0}", obj);
    }

    public static String format(String str, String[] obj) {
        if (str == null || obj == null)
            return str;

        for (int i = 0; i < obj.length; i++)
            str = replace(str, "{" + i + "}", obj[i]);

        return str;
    }

    public static String format(String str, Object... obj) {
        if (str == null || obj == null)
            return str;

        for (int i = 0; i < obj.length; i++)
            str = replace(str, "{" + i + "}", String.valueOf(obj[i]));

        return str;
    }

    public static String replace(String s, char oldSub, char newSub) {
        return replace(s, oldSub, Character.valueOf(newSub));
    }

    public static String replace(String s, char oldSub, String newSub) {
        if ((s == null) || (newSub == null)) {
            return null;
        }

        char[] c = s.toCharArray();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < c.length; i++) {
            if (c[i] == oldSub) {
                sb.append(newSub);
            } else {
                sb.append(c[i]);
            }
        }

        return sb.toString();
    }

    public static String replace(String s, String oldSub, String newSub) {
        if ((s == null) || (oldSub == null) || (newSub == null)) {
            return null;
        }

        int y = s.indexOf(oldSub);

        if (y >= 0) {
            StringBuffer sb = new StringBuffer();
            int length = oldSub.length();
            int x = 0;

            while (x <= y) {
                sb.append(s.substring(x, y));
                sb.append(newSub);
                x = y + length;
                y = s.indexOf(oldSub, x);
            }

            sb.append(s.substring(x));

            return sb.toString();
        } else {
            return s;
        }
    }

    public static String replace(String s, String[] oldSubs,
            String[] newSubs) {
        if ((s == null) || (oldSubs == null) || (newSubs == null)) {
            return null;
        }

        if (oldSubs.length != newSubs.length) {
            return s;
        }

        for (int i = 0; i < oldSubs.length; i++) {
            s = replace(s, oldSubs[i], newSubs[i]);
        }

        return s;
    }

    public static String replace(String s, String[] oldSubs, char[] newSubs) {
        if ((s == null) || (oldSubs == null) || (newSubs == null)) {
            return null;
        }

        if (oldSubs.length != newSubs.length) {
            return s;
        }

        for (int i = 0; i < oldSubs.length; i++) {
            s = replace(s, oldSubs[i], String.valueOf(newSubs[i]));
        }
        return s;
    }

    public static String substring(String str, int beginIndex) {
        if (str == null) {
            return null;
        }

        if (beginIndex < 0) {
            return EMPTY;
        }

        if (beginIndex > str.length()) {
            return EMPTY;
        }

        return str.substring(beginIndex);
    }

    public static String substring(String str, int beginIndex, int endIndex) {
        if (str == null) {
            return null;
        }

        if (beginIndex < 0 || endIndex < 0) {
            return EMPTY;
        }

        if (endIndex > str.length()) {
            endIndex = str.length();
        }

        if (beginIndex > endIndex || beginIndex > str.length()) {
            return EMPTY;
        }

        return str.substring(beginIndex, endIndex);
    }
}

Related

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