How can I use Linq to create IEnumerable based on line breaks in a string?

I have C # code in a line that looks like this:

content = 'var expData = details.Select(x => x.Explanation.TextWithHtml).ToList(); var score = resData.SequenceEqual(ansData); var explanation = "";'; 

How can I do this so that the code is converted to the next using LINQ?

 <td>01</td><td>var expData = details.Select(x => x.Explanation.TextWithHtml ).ToList();</td> <td>02</td><td>var score = resData.SequenceEqual(ansData);</td> <td>03</td><td>var explanation = "";</td> 
+4
source share
2 answers

It looks like you want something like:

 var lines = text.Split(new[] { "\r\n" }, StringSplitOptions.None) .Select((line, index) => string.Format("<td>{0:00}</td><td>{1}</td>", index + 1, EscapeHtml(line))); 

You will need to write EscapeHtml , though - you do not want the tags in your C # codec to end like tags in HTML anyway!

+8
source

This should work, you can get the index from Enumerable.Select :

 IEnumerable<String> code = content.Split(new[] { Environment.NewLine }, StringSplitOptions.None) .Select((line, index) => string.Format("<td>{0}</td><td>{1}</td>", (index + 1).ToString("D2"), line)); 

A practical guide. Copy number with leading zeros

+4
source

Source: https://habr.com/ru/post/1437985/


All Articles