修改列表< string>将内容转换为超链接 [英] Modify List<string> to convert contents to hyperlinks
问题描述
我有一个List<string>
,其中装有URL.我想做的是将List
的内容转换为用户可以单击的超链接.我已经看到了许多有关如何执行此操作的示例,但是大多数示例是插入到电子邮件中,或将 here 单词切换为超链接.我只是不知道我在看什么,所以有点混乱.这是我所拥有的:
I have a List<string>
that get's populated with URLs. What I'd like to do is convert the contents of the List
to hyperlinks that the user can click on. I've seen a bunch of examples of how to do this, but most of them were to insert in to an email, or switch the word here to a hyperlink. I just don't know what I'm looking at, so it's a little confusing. Here's what I have:
List<string> lstUrls = new List<string>();
//PROGRAM GETS URLS FROM ELEMENTS IN HERE....
foreach (string s in lstUrls)
{
s = "<a href=\"%s\"></a>"; //THIS DOESN'T WORK...
}
我不想更改字符串的内容-只是为了能够显示为超链接.例如,一个字符串值将是 https://www.tyco-fire. com/TD_TFP/TFP/TFP172_02_2014.pdf ;以及Stack Overflow如何将其显示为链接,这就是我要完成的工作.
I don't want to change the content of the string - just to be able to display as a hyperlink. For example, one string value will be https://www.tyco-fire.com/TD_TFP/TFP/TFP172_02_2014.pdf; and how Stack Overflow displays it as a link, that's what I would like to accomplish.
我知道我显然在破坏语法.感谢您的帮助.
I know I'm obviously botching the syntax. Any help is appreciated.
推荐答案
使用foreach
进行迭代时,不能更改List<T>
的内容.但是您可以使用for
:
You can´t change the content of a List<T>
while iterating it using foreach
. But you can using for
:
for(int i = 0; i < lstUrls.Count; i++)
{
var s = lstUrls[i];
lstUrls[i] = "<a href=\"" + s + "\">" + s + "</a>";
}
这更容易阅读:
lstUrls[i] = String.Format("<a href=\"{0}\">{0}</a>", s);
这篇关于修改列表< string>将内容转换为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!