Indeed, the string.Replace
method is not versatile enough for your requirements in this case. Lower-level text manipulation should do the job. The alternative is of course regex, but the algorithm I present here is going to be the most efficient method, and I thought it would be helpful to write it anyway to see how you can a lot of text manipulation without regex for a change.
Here's the function.
Update:
- Now works with a
Dictionary<string, string>
instead of a string[]
, which enables a definition to be passed to the function along with the word.
- Now works with arbitrary ordering of definitions dictionary.
...
public static string HtmlReplace(string value, Dictionary<string, string>
definitions, Func<string, string, string> htmlWrapper)
{
var sb = new StringBuilder(value.Length);
int index = -1;
int lastEndIndex = 0;
KeyValuePair<string, string> def;
while ((index = IndexOf(value, definitions, lastEndIndex,
StringComparison.InvariantCultureIgnoreCase, out def)) != -1)
{
sb.Append(value.Substring(lastEndIndex, index - lastEndIndex));
sb.Append(htmlWrapper(def.Key, def.Value));
lastEndIndex = index + def.Key.Length;
}
sb.Append(value.Substring(lastEndIndex, value.Length - lastEndIndex));
return sb.ToString();
}
private static int IndexOf(string text, Dictionary<string, string> values, int startIndex,
StringComparison comparisonType, out KeyValuePair<string, string> foundEntry)
{
var minEntry = default(KeyValuePair<string, string>);
int minIndex = -1;
int index;
foreach (var entry in values)
{
if (((index = text.IndexOf(entry.Key, startIndex, comparisonType)) < minIndex
&& index != -1) || minIndex == -1)
{
minIndex = index;
minEntry = entry;
}
}
foundEntry = minEntry;
return minIndex;
}
And a small test program. (Notice the use of a lambda expression for convenience.)
static void Main(string[] args)
{
var str = "Definition foo; Definition bar; Definition baz";
var definitions = new Dictionary<string, string>();
definitions.Add("foo", "Definition 1");
definitions.Add("bar", "Definition 2");
definitions.Add("baz", "Definition 3");
var output = HtmlReplace(str, definitions,
(word, definition) => string.Format("<dfn title=\"{1}\">{0}</dfn>",
word, definition));
}
Output text:
Definition <dfn title="Definition 1">foo</dfn>; Definition <dfn title="Definition 2">bar</dfn>; Definition <dfn title="Definition 3">baz</dfn>
Hope that helps.