c# - Append <i> tag before and after every fourth set of words -


i have written method below lines of code

public string splitline(string input) {     var wordlist = input.split(' ');     var sb = new stringbuilder();     (int index = 0; index < wordlist.length; index++)     {         sb.append("<i>" + wordlist[0]);         if (index % 4 == 0 && index > 0)             sb.append("<br/></i>" + wordlist[index]);         else             sb.append(wordlist[index] + ' ');     }     return sb.tostring(); } 

basically want should append <i> tag , <br/> after every fourth set of words e.g

if string contains "hello james, web designer profession. love web designing"

then output should

<i>hello james,</i><br/> <i>i web designer</i><br/> <i>by profession. love</i><br/> <i>web designing. </i> 

linq solution (providing "i web designer" error , right 4 words chunk should "i web") colud be:

  string text =      "hello james, web designer profession. love web designing";    string result = string.join(environment.newline, text     .split(' ')     .select((item, index) => new {         text = item,         index = index / 4       })     .groupby(data => data.index, data => data.text)     .select(data => "<i>" + string.join(" ", data) + "</i><br/>")); 

the result contain

<i>hello james,</i><br/> <i>i web</i><br/> <i>designer profession. i</i><br/> <i>love web designing</i><br/> 

Comments