C# 用零向左填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pad left with zeroes
提问by Nickon
I want to pad left every number with zeroes (it has to be 8 digits) in my string.
我想在我的字符串中用零填充每个数字(它必须是 8 位数字)。
e.g.
例如
asd 123 rete > asd 00000123 rete
4444 my text > 00004444 my text
Is it possible to do this using regular expressions? Especially Regex.Replace()?
是否可以使用正则表达式来做到这一点?特别是Regex.Replace()?
Notice that the number of zeroes is different for different numbers. I mean the padded number has to be 8 digits long.
请注意,对于不同的数字,零的数量是不同的。我的意思是填充的数字必须是 8 位数字。
采纳答案by Chris Gessler
Microsoft has built in functions for this:
微软为此内置了函数:
someString = someString.PadLeft(8, '0');
And here's an article on MSDN
这是MSDN上的一篇文章
To use a regular expression, do something like this:
要使用正则表达式,请执行以下操作:
string someText = "asd 123 rete";
someText = Regex.Replace(someText, @"\d+", n => n.Value.PadLeft(8, '0'));
回答by Sean Dawson
If you don't have any attachment to Regex just use format strings:
如果您对 Regex 没有任何附件,只需使用格式字符串:
C# convert int to string with padding zeros?
回答by user3815440
Thread is old but maybe someone needs this.
线程很旧,但也许有人需要这个。
Nickon states he wants to use regex. Why? Doesn't matter, maybe its fun. I had to do a inline replace in SQL so some home made SQL functions that calls a C# regex has been helpful.
Nickon 说他想使用正则表达式。为什么?没关系,也许它很有趣。我不得不在 SQL 中进行内联替换,因此一些调用 C# 正则表达式的自制 SQL 函数很有帮助。
What I needed to pad looked something like this:
我需要填充的东西看起来像这样:
abc 1.1.1
abc 1.2.1
abc 1.10.1
and I wanted:
我想要:
abc 001.001.001
abc 001.002.001
abc 001.010.001
So I could sort it alphabetiacally.
所以我可以按字母顺序排序。
The only solution so far (that I found) was to do the padding and truncating to right length in two steps. I couldn't use a Lambda since this was in SQL and I hadn't prepared my functions for that.
到目前为止(我发现的)唯一的解决方案是分两步进行填充和截断到正确的长度。我无法使用 Lambda,因为这是在 SQL 中,而且我还没有为此准备函数。
//This pads any numbers and truncates it to a length of 8
var unpaddedData = "...";
var paddedData = Regex.Replace(unpaddedData , "(?<=[^\d])(?<digits>\d+)",
"0000000${digits}");
var zeroPaddedDataOfRightLength = Regex.Replace(paddedData ,"\d+(?=\d{8})","");
Explanations:
说明:
(?<=[^\d])(?<digits>\d+)
(?<=[^\d]) Look behind for any non digit, this is needed if there are
more groups of numbers that needs to be padded
(?<digits>\d+) Find the numbers and put them in a group named digits to be
used in the replacement pattern
0000000${digits} Pads all the digits matches with 7 zeros
\d+(?=\d{8}) Finds all digits that are followed by at exactly 8 digits.
?= Doesn't capture the 8 digits.
Regex.Replace(...,"\d+(?=\d{8})","")
Replaces the leading digits with nothing leaving the last 8.
回答by Anas Alhariri
static void Main(string[] args)
{
string myCC = "4556364607935616";
string myMasked = Maskify(myCC);
Console.WriteLine(myMasked);
}
public static string Maskify(string cc)
{
int len = cc.Length;
if (len <= 4)
return cc;
return cc.Substring(len - 4).PadLeft(len, '#');
}
Output:
输出:
######5616Just replace the '#' with 0 or '0'. Hope that helped :)
只需将 '#' 替换为 0 或 '0'。希望有所帮助:)

