开发者

Replacing hyphens in querystring via Regular Expression

Due to a restriction with a URLRewrite module, I am replacing all whitespace in a querystring value with hyphens. Server side I want to replace the hyphens back to whitespace, which is fine.

However, if there is a hyphen in the querystring (before I encode the value), when I decode the querystring, it removes ALL hyphens, include the one which is meant to be there.

So my question is, how do I achieve the following with a Regex/Regex in C#....

Example 1

.................................

Querystr开发者_如何学JAVAing: "a-search-term"

Decoded value: "a search term"

Example 2

.................................

Querystring: "a-hyphenated---search"

Decoded value: "a hyphenated - search"

Also, I'm open for suggestions as to how to handle something like...

Querystring: "up-for--discussion" Decoded value: "up for -discussion"

Many thanks


Try Server.UrlEncode("a search term"), no need to decode, asp.net will get the correct value when reading


Could you do a simple string replace?

This should work for your basic scenarios but it's not the best solution:

string newstring = yourstring.Replace("-", " ").Replace("   ", " - ").Replace("  ", " -");


In that case, try the escape/unescape functions

http://www.webtoolkit.info/javascript-url-decode-encode.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜