[嗨,我只是想在字符串中添加零,例如,某公司的系统中有一些旧条形码,只打印了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填充符,当字符数应该为13并加上零时,它仍然是12个字符。
只需将其添加为字符串?
您必须使用BarCode.PadLeft(BarCode.Length + 1, pad)
来获得所需的输出。但我不明白您为什么要这么做,只需添加"0" + BarCode