Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me:
I have paragraph, copy to textbox:

----------------------------------------------------------------
1. The <title> element 50m 45s
defines a title in the browser toolbar 15m 20s
provides a title for the page when it is added to favorites 15m 10s
displays a title for the page in search-engine results 20m 15s
2. CSS can be added to HTML in the following ways 21m 40s
Inline - using the style attribute in HTML elements 5m 10s
Internal - using the <style> element in the <head> section 10m 3s
External - using an external CSS file 6m 27s
----------------------------------------------------------------

I want format this paragraph with C#, it will delete all time at end of every title row. Then it insert every tag html, when i click button, it will show below in code html:
----------------------------------------------------------------
<ul>
<li>0. The &lt;title&gt; element
<ul>
<li>defines a title in the browser toolbar </li>
<li>provides a title for the page when it is added to favorites </li>
<li>displays a title for the page in search-engine results </li>
</ul></li>
<li>1. CSS can be added to HTML in the following ways
<ul>
<li>Inline - using the style attribute in HTML elements</li>
<li>Internal - using the &lt;style&gt; element in the &lt;head&gt; section</li>
<li>External - using an external CSS file</li>
</ul></li>
</ul>
----------------------------------------------------------------

Thank you very much.
Posted
Updated 7-May-13 21:15pm
v2
Comments
Manfred Rudolf Bihy 8-May-13 3:16am    
Format for what output medium?
lukeer 8-May-13 3:47am    
OP wants to create HTML from some custom structured string.
Radoslav Dimitrov 8-May-13 8:59am    
Yes, I think
lukeer 8-May-13 3:49am    
That's an interesting task.
How is it causing you trouble?
OriginalGriff 8-May-13 4:05am    
And? What part of this is causing you difficulties?
What have you tried? Where are you stuck?

1 solution

You were pretty much there. The following code takes this input

XML
1. The <title> element 50m 45s
defines a title in the browser toolbar 15m 20s
provides a title for the page when it is added to favorites 15m 10s
displays a title for the page in search-engine results 20m 15s
2. CSS can be added to HTML in the following ways 21m 40s
Inline - using the style attribute in HTML elements 5m 10s
Internal - using the <style> element in the <head> section 10m 3s
External - using an external CSS file 6m 27s


and provides this output which from what i gather is your desired output.

XML
<ul>
<li>1. The <title> element 50m 45s</li>
<li>defines a title in the browser toolbar 15m 20s</li>
<li>provides a title for the page when it is added to favorites 15m 10s</li>
<li>displays a title for the page in search-engine results 20m 15s</li>
<li>2. CSS can be added to HTML in the following ways 21m 40s</li>
<li>Inline - using the style attribute in HTML elements 5m 10s</li>
<li>Internal - using the <style> element in the <head> section 10m 3s</li>
<li>External - using an external CSS file 6m 27s</li>
</ul>


I am pretty much using what you had in your comment above.

C#
var inputValue =
                @"1. The <title> element 50m 45s
                    defines a title in the browser toolbar 15m 20s
                    provides a title for the page when it is added to favorites 15m 10s
                    displays a title for the page in search-engine results 20m 15s
                    2. CSS can be added to HTML in the following ways 21m 40s
                    Inline - using the style attribute in HTML elements 5m 10s
                    Internal - using the <style> element in the <head> section 10m 3s
                    External - using an external CSS file 6m 27s";

            var lines = inputValue.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            var buildstr = new StringBuilder();

            buildstr.Append("<ul>" + Environment.NewLine);
            foreach (var line in lines)
            {
                buildstr.AppendFormat("<li>{0}{1}</li>", line.Trim(), Environment.NewLine);
            }
            buildstr.Append("</ul>" + Environment.NewLine);

            Console.WriteLine(buildstr.ToString());




If you desire to remove the times this method using Linq and Regex to remove the times.

C#
var inputValue =
                @"1. The <title> element 50m 45s
                    defines a title in the browser toolbar 15m 20s
                    provides a title for the page when it is added to favorites 15m 10s
                    displays a title for the page in search-engine results 20m 15s
                    2. CSS can be added to HTML in the following ways 21m 40s
                    Inline - using the style attribute in HTML elements 5m 10s
                    Internal - using the <style> element in the <head> section 10m 3s
                    External - using an external CSS file 6m 27s";

            var lines = inputValue.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            var buildstr = new StringBuilder();

            buildstr.Append("<ul>" + Environment.NewLine);
            foreach (string newline in lines.Select(line => Regex.Replace(line, "[0-9]+s", "")).Select(newline => Regex.Replace(newline, "[0-9]+m", "")))
            {
                buildstr.AppendFormat("<li>{0}</li>{1}", newline.Trim(), Environment.NewLine);
            }
            buildstr.Append("</ul>" + Environment.NewLine);

            Console.WriteLine(buildstr.ToString());


Which gives this output

XML
<ul>
<li>1. The <title> element</li>
<li>defines a title in the browser toolbar</li>
<li>provides a title for the page when it is added to favorites</li>
<li>displays a title for the page in search-engine results</li>
<li>2. CSS can be added to HTML in the following ways</li>
<li>Inline - using the style attribute in HTML elements</li>
<li>Internal - using the <style> element in the <head> section</li>
<li>External - using an external CSS file</li>
</ul>
 
Share this answer
 
v2
Comments
Matt T Heffron 8-May-13 16:29pm    
I believe OP wanted to strip the times from the end of every line before building. I think the example html shown was the desired result.
David_Wimbley 9-May-13 10:30am    
Ah oops ok ill redo my submission...i wasn't very clear to begin with...thanks. I've updated the solution to include with/without times
duonglg 10-May-13 2:59am    
In your script, it notice a error at lines.Select, error for this array, it dont have Error 10 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found.
David_Wimbley 10-May-13 9:54am    
I am using Linq, that is where .Select comes from. What version .Net are you using? You would need to add the System.Linq namespace to your project.

http://msdn.microsoft.com/en-us/library/bb548891.aspx
duonglg 10-May-13 1:29am    
Yes, Matt Heffron tell right, output was desired result.
Help me more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900