怎么可以把一个少于两个字符数转化为两个字符
比如   0  10  02    123   2  转化为 00 10 02 123 02 

解决方案 »

  1.   

     int panda = 1;            Console.WriteLine(panda.ToString("D2"));//结果01
      

  2.   


    public string FormatString(string str)
    {
         if(str.length==1)
         {
            str = "0" + str;
         }
         
         return str;
    }
      

  3.   

    string str = "1";
    Console.WriteLine(str.PadLeft(2, '0'));
      

  4.   

    int panda = 1;            Console.WriteLine(panda.ToString("D2"));//结果01
      

  5.   

    str=str.PadLeft(1, '0')//当只有1位长度时 补充1位0;
      

  6.   


    if(1==str.length)
    {
       str.PadLeft(2, '0');
    }
      

  7.   

    迟到
    如果总长度固定的话:
    String..::.PadRight 方法 (Int32, Char) 參數 
    totalWidth 
    型別:System..::.Int32 產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。 paddingChar 
    型別:System..::.Char Unicode 填補字元。 傳回值 
    型別:System..::.String 新的 String,對等於這個執行個體,但是靠左對齊,並在右側填補必要的 paddingChar 字元,以建立 totalWidth 的長度。如果 totalWidth 小於這個執行個體的長度,則為與這個執行個體相同的新 String。 範例 
    C# code 
    string str = "forty-two";
    char pad = '.';Console.WriteLine(str.PadRight(15, pad));    // Displays "forty-two......".
    Console.WriteLine(str.PadRight(2,  pad));    // Displays "forty-two".