源码: /*---------------生成静态页-------------------*/
Hashtable ht = new Hashtable();
ht.Add("$con$", info.ProductIntro);
ht.Add("$title$", info.PageTitle);
foreach (DictionaryEntry de in varTable)
{
//此处应该如何写表达式把$con$替换成info.ProductIntro的内容,把$title$替换成info.pagetitle的内容
}
Hashtable ht = new Hashtable();
ht.Add("$con$", info.ProductIntro);
ht.Add("$title$", info.PageTitle);
foreach (DictionaryEntry de in varTable)
{
//此处应该如何写表达式把$con$替换成info.ProductIntro的内容,把$title$替换成info.pagetitle的内容
}
解决方案 »
- 怎样让别的团购网站收录自己的网站
- 在ASP.NET网站中,能实现对串口数据的接收吗?
- 在 Repeater 中,如何初始化 asp:TextBox 默认值呢,如果默认值为 0,则为只读
- 问一个关于效率的问题 100分求高手解惑
- girdview 列值颜色,怎么写啊
- 初学,不知道为什么有时override protected有时protected override,倒底哪个对,在线,明白后立即给分。
- 如何在.net的datagrid中实现vb的datagrid.fixedcols属性功能
- Asp.net--发送电子邮件剖析之一
- 一个初学者的问题!请指教!
- 请问这个你们是怎么解决的?(ASP.NET)
- 关于模板里的第一次载入的问题
- 答题.每题20分关于SQL和Javascript
string str="<html><head><title>$title$</title><head><body>$content$</body>"
str为原内容.现在通过hashtable把要替换的字符和替换的内容引进来.然后在循环里面把str字符串对应de.key的内容替换成de.valve假如下代码:
string str="<html><head><title>$title$</title><head><body>$content$</body>"
string content="";
Hashtable ht = new Hashtable();
ht.Add("$con$", info.ProductIntro);
ht.Add("$title$", info.PageTitle);
foreach (DictionaryEntry de in varTable)
{
content=regx.replace(str,.....)
//省略号的部分如何写?
}
foreach了也会报错的
Hashtable 是键值对格式
直接这样写就行了
ht["$con$"]=info.ProductIntro;
Hashtable 添加相同键名的时候,他会把这个键值给覆盖了
string str = " <html> <head> <title>$title$</title> <head> <body>$content$</body>";
string content="";
Hashtable ht = new Hashtable();
ht.Add("$title$", "aaaaaa");
ht.Add("$content$", "bbbbbb");
foreach (DictionaryEntry de in ht)
{
str = str.Replace(de.Key.ToString(),de.Value.ToString());
}
reg.Replace(str, delegate(Match m){return ht[m.Value] as string})
就可以了。
第二个参数是一个委托,会把匹配到的值传入到该委托,然后这个方法用委托返回的值替换那个匹配到的字串。
string str = " <html> <head> <title>$title$ </title> <head> <body>$content$ </body>";
string content="";
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add("$content$", "內容");
ht.Add("$title$", "标题");
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\$\w+\$");
content = reg.Replace(str, r => ht[r.Value] as string);
Console.WriteLine(content); //显示:<html> <head> <title>标题 </title> <head> <body>内容 </body>应该是这个意思吧?
Hashtable ht = new Hashtable();
ht.Add("$title$", "aaaaaa");
ht.Add("$content$", "bbbbbb");
foreach (DictionaryEntry de in ht)
{
str=Regex.Replace(str, de.Key.ToString().Replace("$","\\$"), de.Value.ToString());
}
这个没太看懂.这个\W应该会把类似于$aa$也替换掉的吧