字符串按格式拆分的算法问题

Shalves 2010-07-09 06:40:15
其实是用来做伪静态的。

求一段代码,或者算法
要根据相应的html文件名字符串分析出对应带参数的aspx文件的路径来。

section-sid_9-t_0-p_1.html 对应 section.aspx?sid=9&t=0&p=1
section-nid_3-t_1.html 对应 section.aspx?nid=3&t=1
section-cid_1-p_3.html 对应 secion.aspx?cid=1&p=3
section-p_16.html 对应 section.aspx?p=16
seciton-cid_1.html 对应 secion.aspx?cid=1

要是有好的伪静态文件命名格式,能解决问题,也可以。
...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Peter200694013 2010-07-12
  • 打赏
  • 举报
回复

public static String ChangeFormat(String str)
{
Int32 i = str.IndexOf('-');
if (i < 0)
{
return str.Substring(0, str.IndexOf('.')) + ".aspx";
}
else
{
String start = str.Substring(0, i) + ".aspx?";
String temp = str.Substring(i + 1, str.IndexOf('.') - i - 1);
return start + temp.Replace('_', '=').Replace('-','&');
}

}
兔子-顾问 2010-07-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 shalves 的回复:]
引用 4 楼 wuyazhe 的回复:
C# code
string a(string value)
{
string result = Regex.Replace(value, @"\.aspx\?|[=&amp;]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; });
}

不过表达式计算出来的结果有点不太……
[/Quote]

怎么不对你贴出例子。


static void Main(string[] args)
{
string[] test = new string[] { "section.aspx?sid=9&t=0&p=1", "section.aspx?nid=3&t=1", "section.aspx?cid=1&p=3", "section.aspx?p=16", "section.aspx?cid=1" };
foreach (string s in test)
{
Console.WriteLine(a(s) + " 对应 " + s);
}
Console.ReadKey();
}

static string a(string value)
{
return Regex.Replace(value, @"\.aspx\?|[=&]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; })+".html";
}


结果:
section-sid_9_t_0_p_1.html 对应 section.aspx?sid=9&t=0&p=1
section-nid_3_t_1.html 对应 section.aspx?nid=3&t=1
section-cid_1_p_3.html 对应 section.aspx?cid=1&p=3
section-p_16.html 对应 section.aspx?p=16
section-cid_1.html 对应 section.aspx?cid=1


你提问的自己要吧问题描述清楚。而且。你例子是错的。section,有些笔误的,这是笔误还是既定情况?允许secion的?你要说明白。否则写再多都是白写。
Shalves 2010-07-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 thoughter 的回复:]
C# code
string a(string value)
{
return value.Replace(".html", "").Replace("section-", "section.aspx?").Replace("-", "&").Replace("_", "=");
}
[/Quote]

你个不能用
thoughter 2010-07-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 shalves 的回复:]

引用 8 楼 thoughter 的回复:
C# code
string a(string value)
{
return value.Replace(".html", "").Replace("section-", "section.aspx?").Replace("-", "&amp;").Replace("_", "=");
}


你个不能用
[/Quote]
同10楼问
[Quote=引用 10 楼 wuyazhe 的回复:]

引用 6 楼 shalves 的回复:
引用 4 楼 wuyazhe 的回复:
C# code
string a(string value)
{
string result = Regex.Replace(value, @"\.aspx\?|[=&amp;amp;]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; ……
[/Quote]
4个例子都试过,不知道哪里不能用...
thoughter 2010-07-09
  • 打赏
  • 举报
回复
string a(string value)
{
return value.Replace(".html", "").Replace("section-", "section.aspx?").Replace("-", "&").Replace("_", "=");
}
daddi 2010-07-09
  • 打赏
  • 举报
回复
我个人建议在此处不用正则表达式,因为正则表达式比字符串处理(remove,replace等)的速度要慢很多。
很明显,你这是URL重写,几乎每个访问的页面都会用到这个函数,那么性能方面就很重要,宁可写得复杂一点,但是性能要好,不能求简单用正则表达式。
Shalves 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]
C# code
string a(string value)
{
string result = Regex.Replace(value, @"\.aspx\?|[=&]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; });
}
[/Quote]
不过表达式计算出来的结果有点不太对。- -!给改改
Shalves 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]
C# code
string a(string value)
{
string result = Regex.Replace(value, @"\.aspx\?|[=&]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; });
}
[/Quote]

大哥,你这个很nb的,帮我写一个反算的,根据html格式算aspx格式的。
兔子-顾问 2010-07-09
  • 打赏
  • 举报
回复
string a(string value)
{
string result = Regex.Replace(value, @"\.aspx\?|[=&]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; });
}
兔子-顾问 2010-07-09
  • 打赏
  • 举报
回复

string test = "section.aspx?sid=9&t=0&p=1";
string result = Regex.Replace(test, @"\.aspx\?|[=&]", delegate(Match m) { return m.Length == 1 ? "_" : "-"; });
Shalves 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taick 的回复:]
dim str as string="section-sid_9-t_0-p_1.html"
dim i as integer=instr(str,"-")
str=mid(str,1,i-1) & ".aspx?" & mid(str,i+1)
i=instr(str,"_")
str=mid(str,1,i-1) & "=" & mid(str,i+1)……
[/Quote]

这个代码是可以,不过我的意思是通过一种方法能适应上面几种html文件名的格式。
不是只针对某一种格式单独写一段代码

比如我有个方法 string a(string value);
当我调用 a(静态文件名)的时候就能返回相应的动态路径来。
taick 2010-07-09
  • 打赏
  • 举报
回复
dim str as string="section-sid_9-t_0-p_1.html"
dim i as integer=instr(str,"-")
str=mid(str,1,i-1) & ".aspx?" & mid(str,i+1)
i=instr(str,"_")
str=mid(str,1,i-1) & "=" & mid(str,i+1)
...
...
可以吗?
主要是能找到规则吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧