[英]How to extract a string from a string in c#
VS 2015, c#. VS 2015,C#。 I have a string...
我有一串...
string str = "Name;IPAddress";
I want to extract just the IPAddress. 我只想提取IPAddress。 I suspect Regex is the best way to do it but I am unsure.
我怀疑正则表达式是最好的方法,但是我不确定。
Any assistance greatly appreciated. 任何帮助,不胜感激。
Why do you think Regex is the best way? 您为什么认为Regex是最好的方法? Do you also want to validate name and IP address?
您还要验证名称和IP地址吗?
string sInput = "John;127.0.0.1";
string[] arrNameAndIP = sInput.Split(';');
bool bIsInputValid = false;
if(arrNameAndIP.Length == 2)
{
Regex rgxNamePattern = new Regex("^[A-za-z]+$");
bool bIsNameValid = rgxNamePattern.IsMatch(arrNameAndIP[0]);
IPAddress ipAddress;
bool bIsIPValid = IPAddress.TryParse(arrNameAndIP[1], out ipAddress);
bIsInputValid = bIsNameValid && bIsIPValid;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.