Here is the function plus some unit test code
#region " Copyleft "
// Copyright (C) 2008 Clive Chinery
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
namespace Demo
{
using System;
using NUnit.Framework;
/// <summary>
/// IpAddressTest
/// </summary>
[TestFixture]
public class IpAddressTest
{
/// <summary>
/// Test GetBlock1
/// </summary>
[Test]
public void GetBlock1Test1()
{
Assert.AreEqual("123", GetBlock1("123.111.222.103"));
}
/// <summary>
/// Test GetBlock1
/// </summary>
[Test]
[ExpectedException(typeof(ArgumentException))]
public void GetBlock1Test2()
{
Assert.AreEqual("123", GetBlock1("123.111.222"));
}
/// <summary>
/// Get Block 1
/// </summary>
/// <param name="input">IP Address</param>
/// <returns>Block 1</returns>
public string GetBlock1(string input)
{
var work = (string.Empty + input).Split('.');
if (work.Length != 4)
{
throw new ArgumentException("input is not in form xxx.xxx.xxx.xxx");
}
return work[0];
}
}
}
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
See the FAQ on the correct forum to post at, at http://forums.asp.net/p/1337412/2699239.aspx#2699239