VS 2015, c#. I have a string...
string str = "Name;IPAddress";
I want to extract just the IPAddress. I suspect Regex is the best way to do it but I am unsure.
Any assistance greatly appreciated.
You can use Split
string str = "Name;IPAddress";
string[] both = str.Split(';');
string name = both[0];
string ipadd = both[1];
Why do you think Regex is the best way? Do you also want to validate name and IP address?
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.