Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
// -> s == "Duke's Birthday: May 23, 1995"
系统输出格式示例:
// Writes a formatted string to System.out.
System.out.format("Local time: %tT", Calendar.getInstance());
// -> "Local time: 13:34:18"
/**
* Method to "format" an array of objects as a single string, performing two possible kinds of
* formatting:
*
* 1. If the first object in the array is a String, and depending on the number of objects in the
* array, then a very simplified and simple-minded C#-style formatting is done. Format items
* "{0}", "{1}", etc., are replaced by the corresponding following object, converted to string
* (of course). These format items must be as shown, with no fancy formatting tags, and only
* simple string substitution is done.
*
* 2. For the objects in the array that do not get processed by point 1 (perhaps all of them,
* perhaps none) they are converted to String and concatenated together with " - " in between.
*
* @param objectsToFormat Number of objects in the array to process/format.
* @param arrayOfObjects Objects to be formatted, or at least the first objectsToFormat of them.
* @return Formatted string, as described above.
*/
public static String formatArrayOfObjects(int objectsToFormat, Object... arrayOfObjects) {
// Make a preliminary pass to avoid problems with nulls
for (int i = 0; i < objectsToFormat; i++) {
if (arrayOfObjects[i] == null) {
arrayOfObjects[i] = "null";
}
}
// If only one object, just return it as a string
if (objectsToFormat == 1) {
return arrayOfObjects[0].toString();
}
int nextObject = 0;
StringBuilder stringBuilder = new StringBuilder();
// If first object is a string it is necessary to (maybe) perform C#-style formatting
if (arrayOfObjects[0] instanceof String) {
String s = (String) arrayOfObjects[0];
while (nextObject < objectsToFormat) {
String formatItem = "{" + nextObject + "}";
nextObject++;
if (!s.contains(formatItem)) {
break;
}
s = s.replace(formatItem, arrayOfObjects[nextObject].toString());
}
stringBuilder.append(s);
}
// Remaining objects (maybe all of them, maybe none) are concatenated together with " - "
for (; nextObject < objectsToFormat; nextObject++) {
if (nextObject > 0) {
stringBuilder.append(" - ");
}
stringBuilder.append(arrayOfObjects[nextObject].toString());
}
return stringBuilder.toString();
}
6条答案
按热度按时间u59ebvdq1#
答案是:
C#的
相当于Java的
注意从1开始的索引,“s”表示使用.toString()转换为字符串。还有许多其他可用的转换和格式化选项:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
wqlqzqxt2#
看一下String.format和PrintStream.format方法。
两者都基于java.util.Formatter class。
字符串.格式示例:
系统输出格式示例:
ss2ws0br3#
还有
MessageFormat.format()
,它使用.net表示法。w1jd8yoj4#
您也可以简单地使用
%s
表示string,因为index是可选参数。在我看来,上面的例子看起来更干净。
java文档中关于
%s
的说明:如果参数arg为null,则结果为“null”。如果arg实现Formattable,则调用arg.formatTo。否则,通过调用arg.toString()获取结果。
7fhtutme5#
Java中有一个
String.format
,尽管语法与. NET中的略有不同。mrphzbgm6#
这并不是对OP问题的真正回答,但可能对正在寻找一种简单方法来将字符串替换为包含C#样式“格式项”的字符串的其他人有所帮助。
(And如果您感到好奇,我将使用此代码作为Android Log方法的简单 Package 器的一部分,以便在单个日志消息中记录多个内容。)