StringExtensions

Dec 17, 2011 at 12:56 AM

using System.Text.RegularExpressions;

namespace Fox.Silverlight.Extensions
{
    /// <summary>
    /// Extensions to <see cref="System.String"/>.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Determines whether the specified input is integer.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>
        ///   <c>true</c> if the specified input is integer; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsInteger(this string input)
        {
            int test;
            return int.TryParse(input, out test);
        }

        /// <summary>
        /// Removes non-digits from the input.
        /// </summary>
        /// <param name="input">The input.</param>
        public static string ToDigitsOnly(this string input)
        {
            var digitsOnly = new Regex(@"[^\d]");
            return digitsOnly.Replace(input, string.Empty);
        }

        /// <summary>
        /// Prepares a string to be converted to <c>int</c>.
        /// </summary>
        /// <param name="input">The input.</param>
        public static string ToIntString(this string input, string defaultValue = "0")
        {
            var output = defaultValue;
            var array = input.Split('.');

            if (array.Length == 0) return output;
            if (string.IsNullOrEmpty(array[0].Trim())) return output;

            output = array[0].ToDigitsOnly();
            if (string.IsNullOrEmpty(output)) return defaultValue;

            return output;
        }
    }
}

Dec 17, 2011 at 2:21 AM

Hey, you! Add this:

        /// <summary>
        /// Determines whether the specified input is integer.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="secondaryTest">The secondary test.</param>
        /// <returns>
        ///   <c>true</c> if the specified input is integer; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsInteger(this string input, Predicate<int> secondaryTest = null)
        {
            int testValue;
            bool test = int.TryParse(input, out testValue);

            if ((secondaryTest != null) && test) return secondaryTest(testValue);
            else return test;

        }

Feb 2, 2012 at 12:57 AM

Yet another one:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace Fox.Extensions
{
    /// <summary>
    /// Extensions of <see cref="System.String"/>.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Determines whether the specified input is integer.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="secondaryTest">The secondary test.</param>
        /// <returns>
        ///   <c>true</c> if the specified input is integer; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsInteger(this string input, Predicate<int> secondaryTest = null)
        {
            int testValue;
            bool test = int.TryParse(input, out testValue);

            if ((secondaryTest != null) && test) return secondaryTest(testValue);
            else return test;
        }

        /// <summary>
        /// Converts the <see cref="String"/> into digits only.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public static string ToDigitsOnly(this string input)
        {
            var digitsOnly = new Regex(@"[^\d]");
            return digitsOnly.Replace(input, string.Empty);
        }

        /// <summary>
        /// Prepares a string to be converted to <c>int</c>.
        /// </summary>
        /// <param name="input">The input.</param>
        public static string ToIntString(this string input, string defaultValue = "0")
        {
            var output = defaultValue;
            var array = input.Split('.');

            if (array.Length == 0) return output;
            if (string.IsNullOrEmpty(array[0].Trim())) return output;

            output = array[0].ToDigitsOnly();
            if (string.IsNullOrEmpty(output)) return defaultValue;

            return output;
        }

        /// <summary>
        /// Converts the <see cref="System.String"/> into a numeric format for parsing.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns>
        /// Returns a numeric string ready for integer or float parsing.
        /// </returns>
        public static string ToNumericString(this string s)
        {
            return new string(s.Where(i => char.IsDigit(i) || i.Equals('.')).ToArray());
        }
    }
}

Am I treating codeplex like pastebin?

Feb 2, 2012 at 2:01 AM

Unit tests stub:

 

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fox.Xavier.Tests
{
    using Extensions;

    /// <summary>
    /// Tests for <see cref="StringExtensions"/>
    /// </summary>
    [TestClass]
    public class StringExtensionsTests
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        /// <summary>
        /// String extensions tests: should parse to decimal.
        /// </summary>
        [TestMethod]
        public void ShouldParseToDecimal()
        {
            var actual = "$1,000.34".ToNumericString();

            Assert.AreEqual("1000.34", actual, "The expected numeric string is not here.");

            var number = decimal.Parse(actual);

            Assert.AreEqual(1000.34M, number, "The expected decimal number is not here.");
        }
    }
}