c# - 填充未应用于字符串
问题描述
嗨,我只是想在一个字符串数字中添加一个零,例如,一家公司在系统中有一些旧条码,打印时只有 12 个字符,他们的新条码是 13,我只需要添加一个额外的零当它的长度为 12 时。
using System;
public class Program
{
public static void Main()
{
string BarCode="000001661705";
char pad = '0';
if(BarCode.Length==12)
{
BarCode = BarCode.PadLeft(1, pad);
}
Console.WriteLine("Length of barcode" + BarCode.Length);
Console.WriteLine("Barcode=" + BarCode);
}
}
这是.net fillddle,您将看到字符数仍然是12,而应该是13,并在其上添加了零。
解决方案
只需将其添加为字符串?
using System;
public class Program
{
public static void Main()
{
string BarCode="000001661705";
char pad = '0';
if(BarCode.Length==12)
{
BarCode = pad + BarCode;
}
Console.WriteLine("Length of barcode" + BarCode.Length);
Console.WriteLine("Barcode=" + BarCode);
}
}
推荐阅读
- python - 无法在我的虚拟环境中导入 Django
- firebase - Firebase Cloud Firestore 数据库规则数据访问被拒绝
- java - ListItem.onItemClickListener 与 CustomAdapter 无响应
- java - java中如何将RSAPublicKey转换为CaviumRSAPublicKey
- python - 如何评估列表中的列表,这些列表内部具有不同的属性(int,str,list):
- javascript - 使用带有 .reduce() 的递归从嵌套的对象数组中删除特定属性
- javascript - 使用 sinon js 库来模拟测试代码使用的现有函数
- node.js - 无法使用子进程生成通过节点(以编程方式)通过 SSH 连接到服务器
- java - SQLException:列索引超出范围,6 > 5。这是我得到的 MySQL 中的错误
- r - 选择包含特定单词的句子