2011-04-15 83 views
2

需要使用HTML ul & li标签代替较大字符串中的文本项目符号字符“•”。该列表总是在字符串的末尾。C#将项目符号点字符转换为HTML无序LIst

我需要转换成这样:

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

这:

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

这样:

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

这:

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

回答

5

您不需要正则表达式,您可以使用string.Split方法分割字符上的字符串,然后使用StringBuilder构建循环中的HTML。

类似下面应该工作(这不完全测试 - 只是它粗糙刺):

// 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>"); 
+0

对不起杰夫,我接受托默勒格的答案,因为他是第一。直到现在才看到您的代码示例。 – Dzejms 2011-04-15 15:31:40

+3

请先定义? – 2011-04-15 15:33:53

5

没有正则表达式的需要。

  1. 追加"</li></ul>"字符串
  2. 取代"•""<ul><li>"(第一次出现只)
  3. 取代"•""</li><li>"(所有剩余的事件)
相关问题