Permalink
Browse files

More string extension methods added.

Added extension methods for IsMatch, Lines, Words, IsNotBlank,
and IsNotNullOrEmpty, with corresponding unit tests.
  • Loading branch information...
1 parent 4663d43 commit 4c8c370fa287f5d26cbb23005a86cac40cb86477 @jarrettmeyer jarrettmeyer committed Feb 8, 2012
Showing with 164 additions and 1 deletion.
  1. +91 −0 Curiosity.Common.Tests/StringExtensionsTests.cs
  2. +73 −1 Curiosity.Common/StringExtensions.cs
@@ -24,6 +24,15 @@ public void Base64Encode_EncodesString()
}
[Test]
+ [TestCase("Hello, World!", Result = "Hello, World!")]
+ [TestCase(" Hello, World! ", Result = "Hello, World!")]
+ [TestCase("\tHello, \t\t World!\r\n", Result = "Hello, World!")]
+ public string CompressWhitespace_returns_expected_result(string target)
+ {
+ return target.CompressWhitespace();
+ }
+
+ [Test]
public void IsBlank_WhenEmpty_ReturnsTrue()
{
string source = "";
@@ -52,6 +61,59 @@ public void IsBlank_WhenWhitespace_ReturnsTrue()
}
[Test]
+ [TestCase("12345", @"\d+", Result = true)]
+ [TestCase("12345", @"[a-z]+", Result = false)]
+ public bool IsMatch_WithoutOptions_ReturnsExpectedResult(string target, string pattern)
+ {
+ return target.IsMatch(pattern);
+ }
+
+ [Test]
+ [TestCase("Hello", Result = true)]
+ [TestCase(null, Result = false)]
+ [TestCase(" ", Result = false)]
+ [TestCase("", Result = false)]
+ [TestCase(" \t ", Result = false)]
+ public bool IsNotBlank_returns_expected_result(string target)
+ {
+ return target.IsNotBlank();
+ }
+
+ [Test]
+ [TestCase("Hello", Result = true)]
+ [TestCase(null, Result = false)]
+ [TestCase(" ", Result = true)]
+ [TestCase("", Result = false)]
+ [TestCase(" \t ", Result = true)]
+ public bool IsNotNullOrEmpty_returns_expected_result(string target)
+ {
+ return target.IsNotNullOrEmpty();
+ }
+
+ [Test]
+ public void Lines_can_break_on_n()
+ {
+ const string s = "this is\nmy string";
+ var lines = s.Lines();
+ Assert.AreEqual(2, lines.Length);
+ }
+
+ [Test]
+ public void Lines_can_break_on_rn()
+ {
+ const string s = "this is\nmy string\r\nwith another line";
+ var lines = s.Lines();
+ Assert.AreEqual(3, lines.Length);
+ }
+
+ [Test]
+ [TestCase("123", Result = 123)]
+ public int ToInt32_can_cast_to_integer(string target)
+ {
+ return target.ToInt32();
+ }
+
+ [Test]
[TestCase("Hello, World!", Result = "65a8e27d8879283831b664bd8b7f0ad4")]
public string ToMd5_ReturnsExpected(string inString)
{
@@ -74,5 +136,34 @@ public void ToSha512_ConvertsStringToHash()
const string expected = "37e2a786c5dc962fac3cfcf5c72b592339abbd7433148e8ed2dd0fc13f1d7402a53de020cb3b0b75f534b9937e191943f3feff64c88af6fbb3eb9e47dccadfa2";
Assert.AreEqual(expected, s.ToSha512());
}
+
+ [Test]
+ public void Words_can_split_string()
+ {
+ var s = "this is a test";
+ var words = s.Words();
+ Assert.AreEqual("this", words[0]);
+ Assert.AreEqual("test", words[3]);
+ }
+
+ [Test]
+ public void Words_can_split_newline()
+ {
+ var s = "this is a\r\nmore difficult test";
+ var words = s.Words();
+ Assert.AreEqual("this", words[0]);
+ Assert.AreEqual("a", words[2]);
+ Assert.AreEqual("more", words[3]);
+ Assert.AreEqual("test", words[5]);
+ }
+
+ [Test]
+ public void Words_can_remove_punctuation()
+ {
+ var s = "Hello, World!";
+ var words = s.Words();
+ Assert.AreEqual("Hello", words[0]);
+ Assert.AreEqual("World", words[1]);
+ }
}
}
@@ -1,4 +1,6 @@
-using System.Security.Cryptography;
+using System;
+using System.Security.Cryptography;
+using System.Text.RegularExpressions;
namespace Curiosity.Common
{
@@ -45,6 +47,20 @@ public static string Base64Encode(this string source)
return StringUtil.Base64Encode(source);
}
+ public static string CompressWhitespace(this string source, string replacement = " ")
+ {
+ if (source.IsNullOrEmpty()) return "";
+ string result = source.Trim();
+ result = Regex.Replace(result, @"\s+", replacement);
+ return result;
+ }
+
+ public static string Delete(this string source, string toDelete)
+ {
+ var deleted = source.Replace(toDelete, "");
+ return deleted;
+ }
+
/// <summary>
/// Returns true if the source string is null, empty, or only contains whitespace.
/// </summary>
@@ -54,6 +70,46 @@ public static bool IsBlank(this string source)
}
/// <summary>
+ /// Returns true if the source string is a match to the given regular expression pattern.
+ /// </summary>
+ /// <param name="source">Source string to inspect</param>
+ /// <param name="pattern">Pattern to match</param>
+ /// <param name="regexOptions">Regular expression options</param>
+ public static bool IsMatch(this string source, string pattern, RegexOptions regexOptions = RegexOptions.None)
+ {
+ return Regex.IsMatch(source, pattern, regexOptions);
+ }
+
+ /// <summary>
+ /// Returns true if the source string is not blank.
+ /// </summary>
+ public static bool IsNotBlank(this string source)
+ {
+ return !source.IsBlank();
+ }
+
+ /// <summary>
+ /// Returns true if the source string is not null or an empty string.
+ /// </summary>
+ public static bool IsNotNullOrEmpty(this string source)
+ {
+ return !source.IsNullOrEmpty();
+ }
+
+ /// <summary>
+ /// Returns true if the source string is null or empty.
+ /// </summary>
+ public static bool IsNullOrEmpty(this string source)
+ {
+ return string.IsNullOrEmpty(source);
+ }
+
+ public static string[] Lines(this string source)
+ {
+ return source.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
+ }
+
+ /// <summary>
/// Converts the source string to its hashed representation defined by
/// the given hash algorithm.
/// </summary>
@@ -63,6 +119,14 @@ public static string ToHash(this string source, HashAlgorithm hashAlgorithm)
}
/// <summary>
+ /// Parse the given string into an integer.
+ /// </summary>
+ public static int ToInt32(this string source)
+ {
+ return Int32.Parse(source);
+ }
+
+ /// <summary>
/// MD5 hash the given string.
/// </summary>
public static string ToMd5(this string source)
@@ -94,5 +158,13 @@ public static string ToSha512(this string source)
return source.ToHash(sha512);
}
}
+
+ public static string[] Words(this string source)
+ {
+ var result = source.CompressWhitespace();
+ result = Regex.Replace(result, @"[^\w\s]", "");
+ var resultArray = result.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
+ return resultArray;
+ }
}
}

0 comments on commit 4c8c370

Please sign in to comment.