隨便寫著玩的。。。
using System;
namespace KiloNet
{
class test
{
static void Main(string[] args)
{
//空位補零,你會選擇哪種方式?
Console.WriteLine("pad('1',4)={0}",pad("1",4));
Console.WriteLine("pad('12',4)={0}",pad("12",4));
Console.WriteLine("pad('123',4)={0}",pad("123",4));
Console.WriteLine("pad('1234',4)={0}",pad("1234",4));
Console.WriteLine("pad('12345',4)={0}",pad("12345",4));
Console.WriteLine("pad('123456',4)={0}",pad("123456",4));
Console.WriteLine("pad2('1',4)={0}",pad2("1",4));
Console.WriteLine("pad2('12',4)={0}",pad2("12",4));
Console.WriteLine("pad2('123',4)={0}",pad2("123",4));
Console.WriteLine("pad2('1234',4)={0}",pad2("1234",4));
Console.WriteLine("pad2('12345',4)={0}",pad2("12345",4));
Console.WriteLine("pad2('123456',4)={0}",pad2("123456",4));
Console.WriteLine("pad3('1',4)={0}",pad3("1",4));
Console.WriteLine("pad3('12',4)={0}",pad3("12",4));
Console.WriteLine("pad3('123',4)={0}",pad3("123",4));
Console.WriteLine("pad3('1234',4)={0}",pad3("1234",4));
Console.WriteLine("pad3('12345',4)={0}",pad3("12345",4));
Console.WriteLine("pad3('123456',4)={0}",pad3("123456",4));
Console.Read();
}
//正常循環
static string pad(string num, int n) {
int i = num.Length;
while(i++ < n) num = "0" + num;
return num;
}
//遞歸
static string pad2(string num, int n) {
return (num.Length >= n)? num:pad2("0" + num, n);
}
//一次搞定
static string pad3(string num, int n) {
return ("".PadLeft(n, '0') + num).Substring(n+ num.Length - Math.Max(num.Length, n), Math.Max(num.Length, n));
}
}
}
namespace KiloNet
{
class test
{
static void Main(string[] args)
{
//空位補零,你會選擇哪種方式?
Console.WriteLine("pad('1',4)={0}",pad("1",4));
Console.WriteLine("pad('12',4)={0}",pad("12",4));
Console.WriteLine("pad('123',4)={0}",pad("123",4));
Console.WriteLine("pad('1234',4)={0}",pad("1234",4));
Console.WriteLine("pad('12345',4)={0}",pad("12345",4));
Console.WriteLine("pad('123456',4)={0}",pad("123456",4));
Console.WriteLine("pad2('1',4)={0}",pad2("1",4));
Console.WriteLine("pad2('12',4)={0}",pad2("12",4));
Console.WriteLine("pad2('123',4)={0}",pad2("123",4));
Console.WriteLine("pad2('1234',4)={0}",pad2("1234",4));
Console.WriteLine("pad2('12345',4)={0}",pad2("12345",4));
Console.WriteLine("pad2('123456',4)={0}",pad2("123456",4));
Console.WriteLine("pad3('1',4)={0}",pad3("1",4));
Console.WriteLine("pad3('12',4)={0}",pad3("12",4));
Console.WriteLine("pad3('123',4)={0}",pad3("123",4));
Console.WriteLine("pad3('1234',4)={0}",pad3("1234",4));
Console.WriteLine("pad3('12345',4)={0}",pad3("12345",4));
Console.WriteLine("pad3('123456',4)={0}",pad3("123456",4));
Console.Read();
}
//正常循環
static string pad(string num, int n) {
int i = num.Length;
while(i++ < n) num = "0" + num;
return num;
}
//遞歸
static string pad2(string num, int n) {
return (num.Length >= n)? num:pad2("0" + num, n);
}
//一次搞定
static string pad3(string num, int n) {
return ("".PadLeft(n, '0') + num).Substring(n+ num.Length - Math.Max(num.Length, n), Math.Max(num.Length, n));
}
}
}