2011-07-16 12 views
0

JavaとC#の両方で使用するための文字列フォーマットパターン表現を1つ作成する方法を探しています。Java C#の文字列フォーマット指定子の変換

のJava:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html

のC#:http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

+0

Java、C#、またはその両方の組み合わせのどちらを使用しますか。 –

+0

私はフォーマットを気にしない。私は両方のために同じものを使用したいです。 – Ben

+0

IKVMを使用しても問題ありませんか? – luiscubal

答えて

0

次のJavaクラスは、C#でString.Formatの 方法の最も重要な機能を実装しています。 '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(); 
} 
} 
関連する問題