我对 C# 和 ASP.NET 开发非常非常新。
我想做的是查找并替换出现在网页正文中的某些词。每次正文中出现某个词时,我都想将该词转换为链接到我们网站上另一个页面的超链接。
我什至不知道从哪里开始。我找到了在 C# 中执行查找和替换的代码,但我没有找到任何帮助来阅读文档、查找某些字符串并将它们更改为不同的字符串。
请您参考如下方法:
实现此目的的几种方法。
string text = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.";
string augmentedText = text.Replace("provide", "<a href='#provide'>provide</a>");
您也可以使用正则表达式来完成此操作。
这是一个将每个单词转换为大写的示例:
public static string MatchEval(Match m)
{
return m.ToString().ToUpper();
}
static void Main(string[] args)
{
string text = "This is some sample text.";
Console.WriteLine(text);
string result = Regex.Replace(text, @"\w+", new MatchEvaluator(MatchEval));
Console.WriteLine(result);
}
希望这有帮助......祝你好运!
