2010-09-20 17 views

उत्तर

29

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" 
+0

सही उत्तर ने निकोलस गिलाउम दिया। – Alex78191

5

जावा में String.format है, हालांकि वाक्यविन्यास .NET में थोड़ा अलग है।

101

यह करने के लिए 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

+18

मुझे पता था कि मुझे सी # बहुत पसंद है, बहुत बेहतर! शायद ऐसा इसलिए है क्योंकि सी # पहली भाषा है जिसे मैंने सीखा है, लेकिन जावा संस्करण में दिखता है। – skeryl

+1

मुख्य कारण सी # दिखता क्लीनर है, यह जावा की तुलना में नई भाषा है। उन दिनों सिंटेक्टिक चीनी बड़ी नहीं थी (जब तक रूबी प्रमुखता में नहीं आया?)। – rpattabi

23

है MessageFormat.format() जो .net संकेतन का उपयोग करता है।

+1

यह एक बिल्कुल पसंद करता है।नेट – noni

+3

बस ध्यान दें कि MessageFormat.format बिल्कुल काम नहीं करता है, उदाहरण के लिए: MessageFormat.format (" {1}", "# 112233", "कुछ") वापस आ जाएगा " कुछ"। जैसा कि आपने अनुमान लगाया है, समस्या 'char - आपको आपूर्ति करने की आवश्यकता है: " {1}"। अधिक के लिए [संदेशफॉर्मैट क्लास गाइड] (http://developer.android.com/reference/java/text/MessageFormat.html) के लिए पढ़ें। – kape123

+0

यह स्वीकार्य उत्तर होना चाहिए :) –

15

आप स्ट्रिंग के लिए %s का उपयोग भी कर सकते हैं क्योंकि इंडेक्स एक वैकल्पिक तर्क है।

String name = "Jon"; 
int age = 26; 
String.format("%s is %s years old.", name, age); 

यह कम शोर है।

नोट के बारे में जावा प्रलेखन से %s:

तो तर्क आर्ग रिक्त है, तो परिणाम "अशक्त" है। अगर तर्क स्वरूपण लागू करता है, तो arg.format को आमंत्रित किया जाता है। अन्यथा, परिणाम arg.toString() का आह्वान करके प्राप्त किया जाता है।

2

यह वास्तव में ओपी के प्रश्न का उत्तर नहीं है, लेकिन उन लोगों के लिए सहायक हो सकता है जो सी # -स्टाइल "प्रारूप आइटम" वाली स्ट्रिंग में तारों के प्रतिस्थापन करने का एक आसान तरीका ढूंढ रहे हैं।

/** 
    * 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(); 
    } 

(और मामले में आप उत्सुक, मैं एंड्रॉयड के लिए लॉग तरीकों के लिए एक सरल आवरण, के हिस्से के रूप में इस कोड यह आसान एक भी लॉग संदेश में कई चीजों लॉग इन करने के बनाने के लिए उपयोग कर रहा हूँ रहे हैं।)

संबंधित मुद्दे