Java C#字符串格式说明符转换

时间:2011-07-16 12:35:41

标签: c# java string-formatting

我正在寻找一种方法,可以在Java和C#中使用一个字符串格式化模式表示。

Java:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html

C#:http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

1 个答案:

答案 0 :(得分:0)

以下Java类实现了String.Format最重要的功能 C#中的方法。它支持'X'和'D'格式说明符,但不支持'F','G'或'R'。

package com.upokecenter.util;

import java.util.Locale;

public final class DotNetFormatter {
private DotNetFormatter() {
}

public static String format(String formattedText,
        Object... options) {
    return format(Locale.getDefault(),formattedText,options);
}

public static String format(Locale locale, String formattedText,
        Object... options) {
    int numOptions = options.length;
    StringBuilder buffer = new StringBuilder();
    int i;
    for (i = 0; i < formattedText.length(); i++) {
        char c = formattedText.charAt(i);
        if (c == '{') {
            i++;
            if (i < formattedText.length()) {
                c = formattedText.charAt(i);
                if (c == '{') {
                    buffer.append('{');
                } else if (c >= '0' || c <= '9') {
                    int x = (c - '0');
                    i++;
                    while (i < formattedText.length()) {
                        c = formattedText.charAt(i);
                        if (c == ':') {
                            i++;
                            if (x >= numOptions
                                    || i >= formattedText.length())
                                throw new IllegalArgumentException(
                                        "Format string contains a badly numbered argument.");
                            char formatType = formattedText.charAt(i);
                            if (formatType == 'x' || formatType == 'X'
                                    || formatType == 'd'
                                    || formatType == 'D') {
                                i++;
                                if (i >= formattedText.length())
                                    throw new IllegalArgumentException(
                                            "Format string contains a badly numbered argument.");
                                char formatCount = formattedText.charAt(i);
                                if (formatCount == '}') {
                                    switch (formatType) {
                                    case 'x':
                                        buffer.append(String.format(locale,
                                                "%x", options[x]));
                                        break;
                                    case 'X':
                                        buffer.append(String.format(locale,
                                                "%X", options[x]));
                                        break;
                                    case 'd':
                                        buffer.append(String.format(locale,
                                                "%d", options[x]));
                                        break;
                                    case 'D':
                                        buffer.append(String.format(locale,
                                                "%d", options[x]));
                                        break;
                                    }
                                    break;
                                } else if (formatCount < '0'
                                        || formatCount > '9'
                                        || (++i) >= formattedText.length())
                                    throw new IllegalArgumentException(
                                            "Format string contains a badly numbered argument.");
                                else {
                                    if (formattedText.charAt(i) != '}')
                                        throw new IllegalArgumentException(
                                                "Format string contains a badly numbered argument.");
                                    String fmt = "";
                                    switch (formatType) {
                                    case 'x':
                                        fmt = String.format("%%0%cx",
                                                formatCount);
                                        break;
                                    case 'X':
                                        fmt = String.format("%%0%cX",
                                                formatCount);
                                        break;
                                    case 'd':
                                        fmt = String.format("%%0%cd",
                                                formatCount);
                                        break;
                                    case 'D':
                                        fmt = String.format("%%0%cd",
                                                formatCount);
                                        break;
                                    }
                                    buffer.append(String.format(locale,
                                            fmt, options[x]));
                                    break;
                                }
                            } else {
                                throw new IllegalArgumentException(
                                        "Format string contains a badly formatted argument.");
                            }
                        } else if (c == '}') {
                            if (x >= numOptions)
                                throw new IllegalArgumentException(
                                        "Format string contains a badly numbered argument.");
                            buffer.append(String.format(locale, "%s",
                                    options[x]));
                            break;
                        } else if (c >= '0' || c <= '9') {
                            i++;
                            x = x * 10 + (c - '0');
                        } else {
                            throw new IllegalArgumentException(
                                    "Format string contains a badly formatted argument.");
                        }
                    }
                } else {
                    buffer.append('{');
                    buffer.append(c);
                }
            } else {
                buffer.append(c);
            }
        } else {
            buffer.append(c);
        }
    }
    return buffer.toString();
}
}