使用C#从IP地址中删除前导零 [英] Remove leading zeros from IP Address with C#

查看:68
本文介绍了使用C#从IP地址中删除前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似"127.000.000.001"的IP,如何删除前导零以得到"127.0.0.1"? 现在我使用这样的正则表达式

I have an IP like this "127.000.000.001" how can I remove the leading zeros to get this "127.0.0.1"? For now i use regex like this

Regex.Replace("127.000.000.001", "0*([0-9]+)", "${1}")

是否有其他方法可以不使用正则表达式来获得此结果?

Is there any other way to achieve this result without using regex?

我将Visual C#3.0用于此代码

I use visual C# 3.0 for this code

推荐答案

是的,有比使用正则表达式更好的方法.

Yes, there's a much better way than using regular expressions for this.

相反,请尝试 System.Net.IpAddress.

Instead, try the System.Net.IpAddress class.

有一个 ToString()方法,该方法将返回一个IP地址的人类可读版本,以其标准符号表示.这可能就是您想要的.

There is a ToString() method that will return a human-readable version of the IP address in its standard notation. This is probably what you want here.

这篇关于使用C#从IP地址中删除前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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