c# - Inject HTML markup around certain words in a string -


I have these two strings: "some text here" and "some text here"

and I have a collection that contains words that I want to match against the text in the strings. "Some", "text", "here"

If a word matches a certain word in the string (even if it is upper or lower case) I want to take the original word from the string and Around this, some HTML markups like & lt; Dfn title = "Definition of the word" & gt; Original word & lt; / Dfn & gt;

I was playing around with the string. Again () the method is not sure how to match regardless of the case and still how to keep the original word intact (so that I do not replace the word "" or vice versa).

In fact, the string.Replace method is not versatile enough for your needs in this case. Lower-level text manipulation should work. The course is regex of course, but the algorithm present here is going to be the most effective method, and I thought it would be helpful to write anyway how many text you can manipulate without a change Here is the function.

Update:

  1. Now enter a a string Instead of instead of & lt; string, string & gt; , the word with which the function is able to pass a definition Creates
  2. Now definition glossary of arbitrary dealing with the order. Public Static String HtmlReplace (string value, dictionary & lt; string, string & gt; definitions, funk & lt; strings;

String, string> htmlWrapper) {var sb = new stringbiller; Int index = -1; Int lastEndIndex = 0; KeyWellPear & lt; String, string & gt; Def; While {sb.Append (value.Substring (lastEndIndex, index - lastEndIndex)) ((index = indexOf (value, definitions, lastEndIndex, StringComparison.InvariantCultureIgnoreCase, DEF out)) =); Sb.Append (htmlWrapper (def.Key, def.Value)); Last index = index + def.key.length; } Sb.Append (value.Substring (last index, value.Length - final and index)); Return sb.ToString (); } Private static integer indexOf (string text, dictionary & strings; string; string; values; STARTINDEX; , String & gt;); Int Min indices = -1; Int index Foreach (upper entry in values) {if (

And a small test program. (Notice of the use of lambda expression for convenience.)

  static zero main (string [] arg) {var str = "definition foo; definition bar; definition hawk"; Var Definitions = New Dictionary & lt; String, string & gt; (); Definitions. Add ("Foo", "Definition 1"); definition. Add ("bar", "definition2"); definition. Add ("falcon", "definition 3"); Up Product = HtmlReplace (str, Definitions, (word, definition) => String.Format ("& LT; DFN title = \" {1} \ "> {0}  

Output text:

Definition & lt; DFN title = & quot; Definition 1 & quot; & Gt; Foo & lt; / DFN> Definition & lt; Dfn title = & quot; Definition 2 & quot; & Gt; Bar & lt; / Dfn & gt; Definition & lt; DFN title = & quot; Definition 3 & quot; & Gt; Baz & lt; / DFN>.

Hope that helps


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -