zoukankan      html  css  js  c++  java
  • C# 字符串位数不足补零的方法

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

     

    在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。

    PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度
    PadLeft(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足 totalWidth 长度
    示例:
    h = h.PadLeft(2, '0');
    注意第二个参数为 char 类型,所以用单引号,也可以用 Convert.ToChar(string value) 把字符串转换成 char 类型。如果字符串长度大于 1,则使用 str.ToCharArray()[index]。

  • 相关阅读:
    Nacos配置管理-什么是配置中心
    MYSQL count
    贷款
    短视频推荐图书
    前端
    err
    err
    Mysql8安装教程
    err
    err
  • 原文地址:https://www.cnblogs.com/swtool/p/3646938.html
Copyright © 2011-2022 走看看