C# 将项目符号点字符转换为 HTML 无序列表

标签 c# regex

需要用较大字符串中的 HTML ul 和 li 标记替换文本项目符号字符“•”。该列表始​​终位于字符串的末尾。

我需要转换这个:

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

最佳答案

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

像下面这样的东西应该可以工作(这还没有完全测试 - 只是粗略地尝试一下):

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

关于C# 将项目符号点字符转换为 HTML 无序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5678809/

相关文章:

c# - C#显示多张图片

.net - 如何强制两个模式捕获到正则表达式中的同一组

python - 忽略列表中包含括号的单词的最有效(Pythonic)方法是什么?

c# - NetMq 套接字是线程安全的吗?

c# - HttpWebRequest 支持命名管道吗?

c# - 在命令行使用 aspnet_compiler 抑制编译器警告

c# - TextBox LostFocus 事件发生得太频繁

regex - 使用 perl regexp 和 $^R 解析嵌套元组

PHP:将 preg_replace 与 htmlentities 结合使用

正则表达式来消除一个词