Java C#字符串格式说明符转换
我正在寻找一种在Java和C#中都使用单字符串格式模式表示的方法 爪哇:Java C#字符串格式说明符转换,c#,java,string-formatting,C#,Java,String Formatting,我正在寻找一种在Java和C#中都使用单字符串格式模式表示的方法 爪哇: 下面的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 Stri
下面的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();
}
}
package com.upokecenter.util;
导入java.util.Locale;
公共最终类DotNetFormatter{
专用DotNetFormatter(){
}
公共静态字符串格式(字符串格式化文本,
对象…选项){
返回格式(Locale.getDefault(),formattedText,options);
}
公共静态字符串格式(区域设置、字符串格式化文本、,
对象…选项){
int numOptions=options.length;
StringBuilder缓冲区=新的StringBuilder();
int i;
对于(i=0;i='0'| | c=numOptions,则为else
||i>=formattedText.length())
抛出新的IllegalArgumentException(
“格式字符串包含编号错误的参数。”);
char formatType=格式化文本.charAt(i);
如果(formatType='x'| | formatType=='x'
||formatType=='d'
||formatType=='D'){
i++;
如果(i>=formattedText.length())
抛出新的IllegalArgumentException(
“格式字符串包含编号错误的参数。”);
char formatCount=formattedText.charAt(i);
如果(formatCount=='}'){
开关(格式化类型){
案例“x”:
buffer.append(String.format)(区域设置,
“%x”,选项[x]);
打破
案例“X”:
buffer.append(String.format)(区域设置,
“%X”,选项[X]);
打破
案例“d”:
buffer.append(String.format)(区域设置,
“%d”,选项[x]);
打破
案例“D”:
buffer.append(String.format)(区域设置,
“%d”,选项[x]);
打破
}
打破
}否则如果(formatCount<'0'
||formatCount>“9”
||(++i)>=formattedText.length()
抛出新的IllegalArgumentException(
“格式字符串包含编号错误的参数。”);
否则{
if(formattedText.charAt(i)!='}')
抛出新的IllegalArgumentException(
“格式字符串包含编号错误的参数。”);
字符串fmt=“”;
开关(格式化类型){
案例“x”:
fmt=String.format(“%0%cx”,
格式计数);
打破
案例“X”:
fmt=String.format(“%0%cX”,
格式计数);
打破
案例“d”:
fmt=String.format(“%0%cd”,
格式计数);
打破
案例“D”:
fmt=String.format(“%0%cd”,
格式计数);
打破
}
buffer.append(String.format)(区域设置,
fmt,选项[x]);
打破
}
}否则{
抛出新的IllegalArgumentException(
“格式字符串包含格式不正确的参数。”);
}
}else如果(c=='}'){
如果(x>=数值)
抛出新的IllegalArgumentException(
“格式字符串包含编号错误的参数。”);
buffer.append(String.format(区域设置,“%s”),
期权[x]);
打破
}否则,如果(c>='0'| | c,您希望使用哪种格式表示:Java、c#,或两者的组合?我不介意格式。我只想两者都使用相同的格式。使用IKVM被认为是可以接受的吗?IKVM实际上不是一个选项,因为实现是