开发者

C# Convert bullet point chars to HTML Unordered LIst

Need to replace text bullet chars "•" with HTML ul & li tags within a larger string. The list is always at the end of the string.

I need to convert this:

"The 2008 <strong>Ford F-350</strong> Super Duty XL is powered by a 5.four-liter V8 engine combined with a six-speed manual transmission. A 6.4-liter diesel and 6.8-liter gasoline-powered V10 are also available. Automatics are optional. The Super Duty XL is available in regular, Crew Cab and Super Crew styles and in two- and four-wheel drive.  • ABS is s开发者_Python百科tandard for safe, secure stopping power • The front seat is full bench, with recline • Optional packages include a power group and other groups to fit your various needs" 

and this:

"The 2008 <strong>Ford F-350</strong> Super Duty XLT is powered by a 5.4-liter V8 engine combined with a six-speed manual transmission. A 6.4-liter Power Stroke diesel engine and a 6.8-liter V10 are also available, as is a five-speed automatic transmission. The XLT contains all XL content plus chrome exterior trim, premium sound and upgraded seating. • Premium audio includes AM/FM stereo, single-CD/MP3 player and four speakers • Front and rear bumpers and grille surround are chrome for enhanced appearance • Remote keyless entry and perimeter antitheft alarm are standard for increased safety and security "

to this:

"The 2008 &lt;strong&gt;Ford F-350&lt;/strong&gt; Super Duty XL is powered by a 5.four-liter V8 engine combined with a six-speed manual transmission. A 6.4-liter diesel and 6.8-liter gasoline-powered V10 are also available. Automatics are optional. The Super Duty XL is available in regular, Crew Cab and Super Crew styles and in two- and four-wheel drive.  <ul><li>ABS is standard for safe, secure stopping power</li><li>The front seat is full bench, with recline</li><li>Optional packages include a power group and other groups to fit your various needs</li></ul>"

and this:

"The 2008 &lt;strong&gt;Ford F-350&lt;/strong&gt; Super Duty XLT is powered by a 5.4-liter V8 engine combined with a six-speed manual transmission. A 6.4-liter Power Stroke diesel engine and a 6.8-liter V10 are also available, as is a five-speed automatic transmission. The XLT contains all XL content plus chrome exterior trim, premium sound and upgraded seating. <ul><li>Premium audio includes AM/FM stereo, single-CD/MP3 player and four speakers</li><li>Front and rear bumpers and grille surround are chrome for enhanced appearance</li><li>Remote keyless entry and perimeter antitheft alarm are standard for increased safety and security</li></ul>"


You don't need regular expressions for this, you can use the string.Split method to split the string on the character and then build the HTML in a loop with a StringBuilder.

Something like the following should work (this is not fully tested - just a rough stab at it):

// Here original represents the string you're converting from.
string[] splits = original.Split(
    new[] { "•" },
    StringSplitOptions.RemoveEmptyEntries);

var htmlBuilder = new StringBuilder();
for (int i = 0; i < splits.Length; i++)
{
    if (i != 0)
    {
        if (i == 1)
        {
            htmlBuilder.Append("<ul><li>");
        }
        else if (i != splits.Length - 1)
        {
            htmlBuilder.Append("</li><li>");
        }
    }

    htmlBuilder.Append(splits[i]);
}
htmlBuilder.Append("</li><ul>");


No regex needed.

  1. append "</li></ul>" to the string
  2. replace "•" with "<ul><li>" (first occurrence only)
  3. replace "•" with "</li><li>" (all remaining occurrences)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜