0

I want to split a string on new lines and format every line different, depending on the line number and on the beginning of that line.

I found an example wich could be nice for my task(stackoverflow):

@Html.Raw(Html.Encode(Model[i].Services[j].ServiceDescription).Replace("\n", "<br />"))

But this needs still some addition like:

  • first line: big caption, formating it.(<h1> text from that model </h1>)
  • second line: middle Caption, formating it.(<h4> text from that model </h4>)
  • third line: normal text, without formating
  • bigger than third line:if "Model[i].Services[j].ServiceDescription" contains "-" than print <ul><li>Model[i].Services[j].ServiceDescription</li><ul> for every new "-" add it to it like: <ul> <li>Model[i].Services[j].ServiceDescription first "-"text </li><li>Model[i].Services[j].ServiceDescription second "-"text </li> <ul>

And I thought I can go like that:

<h1>@Html.Raw(Html.Encode(Model[i].Services[j].ServiceDescription).Replace("\n", "<br />"))</h1> 

But this would format the entire text with <h1> caption, so I need kind of a for-loop and if to iterate throug the string, may be like this:

no JavaScript, but c# ( I put this into fiddler,because I couldn't format it here)

An example input looks like:

"First line\r\nSecond line\r\nThird line\r\n-First Li_Element\r\n-Second Li_Element\r\nNormal text again."

By the way I found something like this split up a string on new lines.

How can I realize it, I don't know how to put all the single parts together to a working code ?

UPDATE: I did something like that:

@BWHtml.ToParagraphs(Model[i].Services[j].ServiceDescription)

I did something like that:

click

UPDATE(FINAL): final

Do you see any danger in this code regarding security issues or any suggestions in improving performance ?

Community
  • 1
  • 1
user254197
  • 883
  • 2
  • 9
  • 31

1 Answers1

1

It seems that Custom HTML Helper is the way to go for you, creating method CustomHtmlHelper.FormatParagraphs, splitting the given strings by lines, modify it to your needs and return a MvcHtmlString.

For additional information about Creating Custom HTML Helpers.

Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75