C#: Replacing the first plain character in HTML string -


what trying replace first character within html string own custom style new tag. unfortunately unable in general way work of examples.

consider next possible html strings:

string str1 = "hello world"; string str2 = "<p><div>hello</div> world <div>some text</div></p>"; string str3 = "<p>hello <span>world</span></p>"; string str4 = "<p><a href="#">h</a>hello world</p>"; string str5 = "<p>hello world <div>some text</div></p>"; 

the results should be:

str1 = "<span class=\"my-style\">h</span>ello world"; str2 = "<p><div><span class=\"my-style\">h</span>ello</div> world <div>some text</div></p>"; str3 = "<p><span class=\"my-style\">h</span>ello <span>world</span></p>"; str4 = "<p><a href="#'><span class=\"my-style\">h</span></a>hello world</p>"; str5 = "<p><span class=\"my-style\">h</span>ello world <div>some text</div></p>"; 

in results 'h' letter has been changed <span class=\"my-style\">h</span>

could please me ?

you can use following 2 methods. first extracts first word of innertext:

private static string extracthtmlinnertextfirstword(string htmltext) {     //match html tag (opening or closing tags)      // followed successive whitespaces     //consider html text single line      regex regex = new regex("(<.*?>\\s*)+", regexoptions.singleline);      // replace html tags (and consequtive whitespaces) spaces     // trim first , last space      string resulttext = regex.replace(htmltext, " ").trim().split(' ').firstordefault();      return resulttext; } 

note: credits http://www.codeproject.com/tips/477066/extract-inner-text-from-html-using-regex

then, replace first word edited value (which calls extracthtmlinnertextfirstword.

private static string replacehtmlinnertext(string htmltext) {     // first word.     string firstword = extracthtmlinnertextfirstword(htmltext);      // add span around first character of first word.     string replacedfirstword = firstword.replace(firstword[0].tostring(), "<span class=\"my-style\">" + firstword[0] +"</span>");      // replace first occurrence of word.     var regex = new regex(regex.escape(firstword));     string replacedtext = regex.replace(htmltext, replacedfirstword, 1);      return replacedtext; } 

you can call method using following:

private static void main(string[] args) {     string str1 = "hello world";     string str2 = "<p><div>hello</div> world <div>some text</div></p>";     console.writeline("original: " + str1);     console.writeline("edited value: " + replacehtmlinnertext(str1));     console.writeline("original: " + str2);     console.writeline("edited value: " + replacehtmlinnertext(str2));     console.read(); } 

output:

original: hello world  edited value: <span class="my-style">h</span>ello world  original: <p><div>hello</div> world <div>some text</div></p>  edited value: <p><div><span class="my-style">h</span>ello</div> world <div>some text</div></p> 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -