gpt4 book ai didi

c# - 如何使用维基百科中的超链接以编程方式制作 html 文本?

转载 作者:太空狗 更新时间:2023-10-29 23:16:22 24 4
gpt4 key购买 nike

我有一个字符串数组,我必须将超链接添加到数组中的项目和给定字符串的每个匹配项。主要类似于维基百科。

类似于:

private static string AddHyperlinkToEveryMatchOfEveryItemInArrayAndString(string p, string[] arrayofstrings)

{ }

string p = "The Domesday Book records the manor of Greenwich as held by Bishop Odo of Bayeux; his lands were seized by the crown in 1082. A royal palace, or hunting lodge, has existed here since before 1300, when Edward I is known to have made offerings at the chapel of the Virgin Mary.";

arrayofstrings = {"Domesday book" , "Odo of Bayeux" , "Edward"};

returned string = @"The <a href = "#domesday book">Domesday Book</a> records the manor of Greenwich as held by Bishop <a href = "#odo of bayeux">Odo of Bayeux</a>; his lands were seized by the crown in 1082. A royal palace, or hunting lodge, has existed here since before 1300, when <a href = "#edward">Edward<a/> I is known to have made offerings at the chapel of the Virgin Mary.";

这样做的最佳方式是什么?

最佳答案

你可能很容易这样做:

foreach(string page in arrayofstrings)
{
p = Regex.Replace(
p,
page,
"<a href = \"#" + page.ToLower() + "\">$0</a>",
RegexOptions.IgnoreCase
);
}
return p;

如果 anchor 的大小写可以和匹配的文本一样,你甚至可以去掉for循环:

return Regex.Replace(
p,
String.Join("|", arrayofstrings),
"<a href = \"#$0\">$0</a>",
RegexOptions.IgnoreCase
);

现在模式变成了 Domesday book|Odo of Bayeux|Edward,不管大小写都会找到匹配项,找到的任何内容都会放回链接文本和 href 属性。

关于c# - 如何使用维基百科中的超链接以编程方式制作 html 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13435149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com