क्या जावा में .NET के String.Format
के बराबर है?जावा समतुल्य .NET के स्ट्रिंग। Format
उत्तर
String.format और PrintStream.format विधियों पर एक नज़र डालें।
दोनों java.util.Formatter class पर आधारित हैं।
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"
जावा में String.format
है, हालांकि वाक्यविन्यास .NET में थोड़ा अलग है।
यह करने के लिए 10 प्रतिशत जवाब है:
सी # के
String.Format("{0} -- {1} -- {2}", ob1, ob2, ob3)
1 आधारित सूचकांक करने के लिए जावा के
String.format("%1$s -- %2$s -- %3$s", ob1, ob2, ob3)
नोट बराबर है, और "एस" स्ट्रिंग के लिए कनवर्ट करने के लिए .toString का उपयोग कर का मतलब है()। वहाँ कई अन्य रूपांतरण उपलब्ध और स्वरूपण विकल्प हैं:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
मुझे पता था कि मुझे सी # बहुत पसंद है, बहुत बेहतर! शायद ऐसा इसलिए है क्योंकि सी # पहली भाषा है जिसे मैंने सीखा है, लेकिन जावा संस्करण में दिखता है। – skeryl
मुख्य कारण सी # दिखता क्लीनर है, यह जावा की तुलना में नई भाषा है। उन दिनों सिंटेक्टिक चीनी बड़ी नहीं थी (जब तक रूबी प्रमुखता में नहीं आया?)। – rpattabi
है MessageFormat.format()
जो .net संकेतन का उपयोग करता है।
यह एक बिल्कुल पसंद करता है।नेट – noni
बस ध्यान दें कि MessageFormat.format बिल्कुल काम नहीं करता है, उदाहरण के लिए: MessageFormat.format (" {1}", "# 112233", "कुछ") वापस आ जाएगा " कुछ"। जैसा कि आपने अनुमान लगाया है, समस्या 'char - आपको आपूर्ति करने की आवश्यकता है: " {1}"। अधिक के लिए [संदेशफॉर्मैट क्लास गाइड] (http://developer.android.com/reference/java/text/MessageFormat.html) के लिए पढ़ें। – kape123
यह स्वीकार्य उत्तर होना चाहिए :) –
आप स्ट्रिंग के लिए %s
का उपयोग भी कर सकते हैं क्योंकि इंडेक्स एक वैकल्पिक तर्क है।
String name = "Jon";
int age = 26;
String.format("%s is %s years old.", name, age);
यह कम शोर है।
नोट के बारे में जावा प्रलेखन से %s
:
तो तर्क आर्ग रिक्त है, तो परिणाम "अशक्त" है। अगर तर्क स्वरूपण लागू करता है, तो arg.format को आमंत्रित किया जाता है। अन्यथा, परिणाम arg.toString() का आह्वान करके प्राप्त किया जाता है।
यह वास्तव में ओपी के प्रश्न का उत्तर नहीं है, लेकिन उन लोगों के लिए सहायक हो सकता है जो सी # -स्टाइल "प्रारूप आइटम" वाली स्ट्रिंग में तारों के प्रतिस्थापन करने का एक आसान तरीका ढूंढ रहे हैं।
/**
* 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();
}
(और मामले में आप उत्सुक, मैं एंड्रॉयड के लिए लॉग तरीकों के लिए एक सरल आवरण, के हिस्से के रूप में इस कोड यह आसान एक भी लॉग संदेश में कई चीजों लॉग इन करने के बनाने के लिए उपयोग कर रहा हूँ रहे हैं।)
- 1. जावा में .NET के वेब क्लाइंट और HttpWebRequest की समतुल्य?
- 2. जावा के समतुल्य .NET के मैनुअल रीसेट इवेंट और वेटहैंडल
- 3. जावा के नंबरफॉर्मैट अपवाद के .NET समतुल्य क्या है?
- 4. जावा इनवेरिएंट संस्कृति के समतुल्य
- 5. स्निपिंग टूल के .NET समतुल्य
- 6. प्रतिबिंब के जावा समतुल्य।
- 7. जावा के स्ट्रिंग रीडर के लिए रूबी समतुल्य क्या है?
- 8. जावा में स्ट्रिंगबफर के .NET समतुल्य क्या है?
- 9. इंटरप्टेड एक्सेप्शन (जावा) के .NET समतुल्य क्या है?
- 10. जावा में @Deprecated के .NET समतुल्य क्या है?
- 11. .NET से AggregateException के जावा समतुल्य क्या है?
- 12. स्ट्रिंग। Format NumberFormatInfo को अनदेखा करता है?
- 13. .NET JAXB समतुल्य?
- 14. जावा में स्पिंटफ समतुल्य
- 15. d3.format
- 16. सी # जावा संसाधन के समतुल्य
- 17. जावा के लिए Yacc समतुल्य
- 18. जेएमएक्स के .NET समतुल्य क्या है?
- 19. क्या जावा वेब स्टार्ट का .NET समतुल्य है?
- 20. जावा सिस्टम के बाहर विजुअल स्टूडियो समतुल्य
- 21. जावा में फ़ाइल.सेपरेटर के समतुल्य सी # समतुल्य है
- 22. पायथन का ".format" फ़ंक्शन
- 23. जावा मानचित्र समतुल्य सी #
- 24. सी # जावा के अपवाद के समतुल्य .printStackTrace()?
- 25. पायथन 3:% s और .format()
- 26. पायथन: यूनिकोड-एस्केड स्ट्रिंग पर .format() का उपयोग
- 27. MomentJS .format() मेरे टाइमज़ोन
- 28. hadoop java.io.IOException: namenode -format
- 29. क्या LINQ के लिए जावा समतुल्य है?
- 30. सी ++ में जावा इंटरफेस के समतुल्य?
सही उत्तर ने निकोलस गिलाउम दिया। – Alex78191