Reputation: 5600
I have a string with carriage returns in it I separate them using the following split:
string[] RemoveEmptySpace = result.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
so it becomes like this:
Server: xxx
Address: xxx.xxx.11.10
Non-authoritative answer:
Name: test.com
Address: xxx.xxx.888.555
now my question is how do i get the first instance of Address numbers only not the text e.g.'xxx.xxx.11.10'? the string comes from nslookup so it is dynamic the position may change...
Thanks
Upvotes: 1
Views: 2068
Reputation: 6365
Alternatively to the split approach, you can use a regular expression:
Regex addrParser = new Regex(@"Address:\s+([\d\.]+)", RegexOptions.Multiline);
string address = null;
if (addrParser.IsMatch(result)) {
address = addrParser.Match(result).Groups[1].Value;
}
This works as intended:
r = new Regex(@"Address:\s+([\d\.]+)", RegexOptions.Multiline);
r.IsMatch("Address: 123.456.888.555"); // true
It is useful to look into a cheat sheet: http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
Upvotes: 0
Reputation: 2155
Use this function:
static String getAddress(String[] Haystack)
{
foreach(String line in Haystack)
{
if(line.Substring(0, 8) == "Address:")
return line.Substring(10);
}
return "";
}
You can call it like this:
getAddress(RemoveEmptySpace)
Upvotes: 0
Reputation: 8613
You could iterate over the collection of strings you got from the split, and parse those beginning with "Address". i.e. something like this:
string[] RemoveEmptySpace = ...;
// ...
foreach (string line in RemoveEmptySpace)
{
if (line.StartsWith("Address:"))
{
string[] segments = line.Split(new char[1] { ':' }, StringSplitOptions.RemoveEmptyEntries);
// Use the second segment, as this should be the value
string value = segments[1].Trim();
// Note: You now have the string representation of the Address numbers.
// Stop searching for more addresses
break;
}
}
Upvotes: 1
Reputation: 10326
Regular expressions are your friend. Assuming your result always contains the phrase "Address: nnn.nnn.nnn.nnn"
, you can simply do:
Match m = Regex.Match(result, "Address\\:\\s*(\\d+(\\.\\d+)+)");
if (m.Success) {
string address = m.Groups[1]; // m.Groups[1] returns the dotted address part
}
You could perform a match for each element in your array, but it's probably easier just to use the result
string.
Upvotes: 2
Reputation: 40536
After you do the split, do this:
var firstAddress =
(from line in RemoveEmptySpace
let split = line.Split(new[] { ':' })
where split[0] == "Address"
select split[1]).First();
(you may need to add using System.Linq;
at the beginning of the file for this to work)
Upvotes: 1
Reputation: 176896
Just break the string in two parts (i.e., apply the Split(...)
function again on the created strings).
string[] add= arrayStr[1].Split(':');
Console.WriteLine("address" + add[1]); // TODO: verify syntax
Upvotes: 2
Reputation: 13872
Iterate over the array RemoveEmptySpace
Check, if the string at current index, StartsWith 'Address:'
if yes, split it with :
and fetch value at index [1]
break
the iteration.
Upvotes: 0