Skip to content

Commit 996ffd1

Browse files
authored
Fix SubstringBetween length calculation (#29)
1 parent c5ddc36 commit 996ffd1

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/DotnetDocument/Extensions/StringExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public static string SubstringBetween(this string text, string start, string end
2727
var endIndex = text.IndexOf(end, StringComparison.Ordinal);
2828

2929
if (text.Contains(start) && text.Contains(end) && startIndex < endIndex)
30-
return text.Substring(startIndex + start.Length, endIndex - end.Length - startIndex);
30+
return text.Substring(startIndex + start.Length,
31+
endIndex - startIndex - start.Length);
3132

3233
return string.Empty;
3334
}

test/DotnetDocument.Tests/StringExtensionsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ public void ShouldOnlySubstringDoubleTriangleBrackets(string text, string expect
2727
substring.ShouldBe(expected);
2828
}
2929

30+
[Fact]
31+
public void ShouldSubstringWithDifferentTokenLengths()
32+
{
33+
const string text = "This is <!--some comment--> in text.";
34+
35+
// Act
36+
var substring = text.SubstringBetween("<!--", "-->");
37+
38+
// Assert
39+
substring.ShouldBe("some comment");
40+
}
41+
3042
}
3143
}

0 commit comments

Comments
 (0)