• 空位補零,你會選擇哪種方式?


    隨便寫著玩的。。。

    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));
            }

        }
    }

    id 博主 = [[KILONET.CNBLOGS.COM alloc] initWithValue:@"天堂向右,我依然向左"

                  网名:@"老舟"

                  兴趣:@"影音,阅读"

                  动态:@"系统架构设计,Android通信模块开发"

                  网址:@"http://kilonet.cnblogs.com"
                  签名:@"--------------------------------------------------

                                  Stay Hungry , Stay Foolish

                                  求  知  若  渴,处  事  若  愚

                              --------------------------------------------------"

                  ];         // Never Release

  • 相关阅读:
    JavaScript的关键点
    博客开始
    CCF
    java-CCF+杂七杂八
    【软件工程】需求分析V2再整理
    汇编 书上实验
    组原2
    chrome主页被绑架
    【软件工程】乱的一遭
    【我希望我能鼓起勇气】汇编语言
  • 原文地址:https://www.cnblogs.com/KiloNet/p/1558840.html
Copyright © 2020-2023  润新知