字符串Trims最左边的Zero [英] To string Trims leftmost Zero

查看:90
本文介绍了字符串Trims最左边的Zero的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Convert.tostring()修剪最左边0



ex。

int x = 01234

Convert.tostring(x)

结果为1234而不是01234

任何建议

谢谢

Convert.tostring() trims leftmost 0

ex.
int x= 01234
Convert.tostring(x)
results into 1234 and not 01234
Any suggestions
Thanks

推荐答案

整数值忽略所有前导零:01234与1234相同,与00000000000000000000000000001234相同

您需要格式化ToString操作:

Integer values ignore all leading zeros: 01234 is teh same as 1234, is the same as 00000000000000000000000000001234
You need to format the ToString operation:
int x = 01234;
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString("D5"));

将打印:

Will print:

1234
01234


整数变量不会考虑前导零

如果您将

01234 分配给整数,那么

变量它将自动保存在 x 变量为 1234



所以,添加将x转换为字符串后的前导零

integer variable will not consider Leading zeroes
so that
if you will assign
01234 to integer variable it will automatically save it in x variable as 1234

so, add leading zeros after converting x to string
string myString = String.Format("{0:00000}",x) // this will add leading zeros in a way that passed number will be have minimum 5 digits e.g. 123 -> 00123



快乐编码!

:)


Happy coding!
:)


你可以用 TrimStart()。所以当你把它转换成字符串时

You can use TrimStart(). So when you converted it to string
string _YourText = "01234";
_YourText = _YourText.TrimStart(new Char[] { '0' } );



- 或 -


--or--

_YourText = _YourText.TrimStart('0');



MSDN中的String.TrimStart方法 [ ^ ]

< br $> b $ b祝你好运,

OI


String.TrimStart Method In MSDN[^]

Good luck,
OI


这篇关于字符串Trims最左边的Zero的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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