Usage
string foo = "I got this from Just Snippets!";
string bar = foo.AddMarkup("Just Snippets", "", "");
would set bar to "I got this from Just Snippets!".
Snippet
namespace My.Helpers
{
// String extension methods.
public static class StringExtensions
{
public static string AddMarkup(this string text, string value, string before, string after, bool ignoreCase = true)
{
string work = text;
int start = 0;
System.StringComparison caseSensitivity;
int bump = value.Length + before.Length + after.Length;
if (ignoreCase)
caseSensitivity = System.StringComparison.CurrentCultureIgnoreCase;
else
caseSensitivity = System.StringComparison.CurrentCulture;
while (true)
{
int i = work.IndexOf(value, start, caseSensitivity);
if (i < 0) break;
work = work.Left(i) + before + work.Substring(i, value.Length) + after
+ work.Right(work.Length - i - value.Length);
start += bump;
}
return work;
}
No comments:
Post a Comment