• 截取字符串


    int i=10;
    方法1:Console.WriteLine(i.ToString("D5"));
    方法2:Console.WriteLine(i.ToString().PadLeft(5,'0'));//推荐
    方法3:Console.WriteLine(i.ToString("00000")); 

    String.PadLeft 方法 
    [C#]请参见
    String 类 | String 成员 | System 命名空间
    右对齐此实例中的字符,在左边用空格或指定的 Unicode 字符填充以达到指定的总长度。
    重载列表
    
    (一)右对齐此实例中的字符,在左边用空格填充以达到指定的总长度。
    
    [Visual Basic] Overloads Public Function PadLeft(Integer) As String
    [C#] public string PadLeft(int);
    [C++] public: String* PadLeft(int);
    [JScript] public function PadLeft(int) : String;
    
    (二)右对齐此实例中的字符,在左边用指定的 Unicode 字符填充以达到指定的总长度。
    
    [Visual Basic] Overloads Public Function PadLeft(Integer, Char) As String
    [C#] public string PadLeft(int, char);
    [C++] public: String* PadLeft(int, __wchar_t);
    [JScript] public function PadLeft(int, Char) : String;
    

    文件重新命名

     static void Main(string[] args)
            {
                string path = @"d:4.txt"; //源文件
                string path2 = @"d:move13.txt";//移动到哪里
                if (MoveFile(path, path2))
                    Console.WriteLine("success");
                else
                    Console.WriteLine("bad");
    
                Console.WriteLine();
            }
            private static bool MoveFile(string OldPath, string MoveNewPath)
            {
                Boolean IsSuccess = false;//状态
    
                try
                {
                    if (!File.Exists(OldPath))//判断路径是是否存在
                    {
                        Console.WriteLine("不存在,正在创建");
                        using (FileStream fs = File.Create(OldPath)) { } //创建文件
                    }
                    if (!File.Exists(MoveNewPath))//判断路径是是否存在
                    {
                        Console.WriteLine("不存在,正在创建");
                        using (FileStream fs = File.Create(MoveNewPath)) { } //创建文件
                    }
                    if (File.Exists(MoveNewPath))
                    {
                        Console.WriteLine("原目录有相同的文件,/r是否覆盖该文件,确定:Y 取消输入:N");
                        string inputVlaue = Console.ReadLine();
                        switch (inputVlaue.ToUpper())
                        {
                            case "Y":
                                File.Delete(MoveNewPath);
                                File.Move(OldPath, MoveNewPath);
                                IsSuccess = true;
                                Console.WriteLine("成功将{0}move到{1}.", OldPath, MoveNewPath);
                                break;
                            case "N":
                                IsSuccess = false;
                                break;
                            default:
                                Console.WriteLine("输入有误");
                                break;
                        }
                    }
                    else
                    {
                        File.Move(OldPath, MoveNewPath);
                        IsSuccess = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }
                return IsSuccess;
            }
    
  • 相关阅读:
    C#中使用OpenSSL的公钥加密/私钥解密
    .NET跨平台:在Linux Ubuntu上编译coreclr/corefx/dnx(20150617)
    ASP.NET Web API与Owin OAuth:调用与用户相关的Web API
    ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
    在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
    C# 中 async/await 调用传统 Begin/End 异步方法
    将GitLab的数据库导入阿里云PostgreSQL RDS
    在Mac/Linux/Windows上编译corefx遇到的问题及解决方法
    在Mac上用自己编译出的DNX运行.NET程序
    [DNX]解决dnu restore时找不到Newtonsoft.Json的问题
  • 原文地址:https://www.cnblogs.com/tested/p/3287076.html
Copyright © 2020-2023  润新知