[英]Java Equivalent to .NET's String.Format
Java中有.NET的String.Format
嗎?
對此的10美分答案是:
C#的
String.Format("{0} -- {1} -- {2}", ob1, ob2, ob3)
相當於Java的
String.format("%1$s -- %2$s -- %3$s", ob1, ob2, ob3)
注意基於1的索引,“s”表示使用.toString()轉換為字符串。 還有許多其他轉換和格式選項:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
看看String.format和PrintStream.format方法。
兩者都基於java.util.Formatter類 。
String.format示例:
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"
System.out.format示例:
// Writes a formatted string to System.out.
System.out.format("Local time: %tT", Calendar.getInstance());
// -> "Local time: 13:34:18"
MessageFormat.format()
使用.net表示法。
您也可以簡單地將%s
用於字符串,因為索引是一個optionnal參數。
String name = "Jon";
int age = 26;
String.format("%s is %s years old.", name, age);
它不那么吵了。
請注意java文檔中的%s
:
如果參數arg為null,則結果為“null”。 如果arg實現了Formattable,則調用arg.formatTo。 否則,通過調用arg.toString()獲得結果。
Java中有一個String.format
,雖然語法與.NET略有不同。
這不是OP問題的真正答案,但對於那些正在尋找一種簡單的方法來將字符串替換為包含C#式“格式項”的字符串的人來說,這可能會有所幫助。
/**
* 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();
}
(如果你很好奇,我正在使用這段代碼作為Android Log方法的簡單包裝器的一部分,以便更容易在單個日志消息中記錄多個內容。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.