Click here to Skip to main content
14,668,844 members
Rate this:
Please Sign up or sign in to vote.
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?
duonglg 8-May-13 6:19am
   
That right? Out put is code html.
Now, I input paragraph:
-------
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

-------
and out put

<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>

i cant do continue for delete time end of row and insert tag ul to first every chapter.
duonglg 8-May-13 9:54am
   
string strImport;
strImport = tarImportLink.Text.ToString();
string[] lines = strImport.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
StringBuilder s = new StringBuilder();

for (int i = 0; i < lines.Length; i++)
{
string firstLi="<li>";
string endLi = "</li>";
s.AppendFormat("{0}{1}{2}{3}",firstLi,lines[i],endLi, Environment.NewLine);
}

string newString = s.ToString();
tarExportLink.EditValue = newString;


I do it now....
Please help me continue

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 3

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

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.

<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.

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.

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

<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>
   
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.
duonglg 10-May-13 1:31am
   
Output right:
<ul>
<li>0. The <title> 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 <style> element in the <head> section</li>
<li>External - using an external CSS file</li>
</ul></li>
</ul>
David_Wimbley 10-May-13 9:58am
   
So wait...what you just posted is the correct/desired output? That is not even properly formatted HTML...

<ul>
<li>0. The <title> 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> <---This is an orphaned element

//This block is not even wrapped in <ul></ul>
<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 <style> element in the <head> section</li>
<li>External - using an external CSS file</li>
</ul>
</li>
</ul> <---This is an orphaned element



So unless something changed in HTML where this will work...i don't know how to not correctly format your HTML. Right now you've got an arbitrary </li> (close li tag) and an arbitrary </ul> (close ul tag).
duonglg 12-May-13 9:53am
   
That code html is right, i have tested it on my program much time.
Your code build get result:

<ul>
<li>1. The <title> element
defines a title in the browser toolbar
provides a title for the page when it is added to favorites
displays a title for the page in search-engine results
2. CSS can be added to HTML in the following ways
Inline - using the style attribute in HTML elements
Internal - using the <style> element in the <head> section
External - using an external CSS file</li>
</ul>

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100