垫留下了零 [英] Pad left with zeroes

查看:106
本文介绍了垫留下了零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要垫留下了零(它必须是8位数字)在我的字符串中的每个号码。

I want to pad left every number with zeroes (it has to be 8 digits) in my string.

例如

asd 123 rete > asd 00000123 rete
4444 my text > 00004444 my text

是否有可能做到这一点使用正则表达式?尤其是 Regex.Replace()

Is it possible to do this using regular expressions? Especially Regex.Replace()?

请注意,零数对不同的号码不同。我的意思是填充的数量已是8位数

Notice that the number of zeroes is different for different numbers. I mean the padded number has to be 8 digits long.

推荐答案

微软已经内置功能如下:

Microsoft has built in functions for this:

someString = someString.PadLeft(8, '0');

和这里的一对的 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'));

这篇关于垫留下了零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆