[hyena] [Hyena] Add SubstringBetween method to StringUtil
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hyena] [Hyena] Add SubstringBetween method to StringUtil
- Date: Sun, 15 Aug 2010 22:04:54 +0000 (UTC)
commit 81e8e6b096d7eedd4165d72e68695e0d26fd902a
Author: Gabriel Burt <gabriel burt gmail com>
Date: Sun Aug 15 15:04:28 2010 -0700
[Hyena] Add SubstringBetween method to StringUtil
Hyena/Hyena/StringUtil.cs | 15 +++++++++++++++
Hyena/Hyena/Tests/StringUtilTests.cs | 10 ++++++++++
2 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/Hyena/Hyena/StringUtil.cs b/Hyena/Hyena/StringUtil.cs
index 8075a8c..99aaac5 100644
--- a/Hyena/Hyena/StringUtil.cs
+++ b/Hyena/Hyena/StringUtil.cs
@@ -329,6 +329,21 @@ namespace Hyena
}
}
+ public static string SubstringBetween (this string input, string start, string end)
+ {
+ int s = input.IndexOf (start);
+ if (s == -1)
+ return null;
+
+ s += start.Length;
+ int l = Math.Min (input.Length - 1, input.IndexOf (end, s)) - s;
+ if (l > 0 && s + l < input.Length) {
+ return input.Substring (s, l);
+ } else {
+ return null;
+ }
+ }
+
private static readonly char[] escaped_like_chars = new char[] {'\\', '%', '_'};
public static string EscapeLike (string s)
{
diff --git a/Hyena/Hyena/Tests/StringUtilTests.cs b/Hyena/Hyena/Tests/StringUtilTests.cs
index e7c940c..637c027 100644
--- a/Hyena/Hyena/Tests/StringUtilTests.cs
+++ b/Hyena/Hyena/Tests/StringUtilTests.cs
@@ -159,6 +159,16 @@ href=http://lkjdflkjdflkjj>baz foo< /a> bar"));
s = new string [] { "foo", "bar", "baz" };
Assert.AreEqual ("foo -- bar -- baz", s.Join (" -- "));
}
+
+ [Test]
+ public void TestSubstringBetween ()
+ {
+ Assert.AreEqual ("bar", "foobarbaz".SubstringBetween ("foo", "baz"));
+ Assert.AreEqual ("barfoobam", "erefoobarfoobambazabc".SubstringBetween ("foo", "baz"));
+ Assert.AreEqual (null, "foobar".SubstringBetween ("foo", "baz"));
+ Assert.AreEqual (null, "bar".SubstringBetween ("foo", "baz"));
+ Assert.AreEqual (null, "".SubstringBetween ("foo", "baz"));
+ }
}
[TestFixture]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]