tags:

views:

307

answers:

3

Hi all,

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
.................................
Querystring: "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

+2  A: 

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

Sergio
Thanks for your reply. However spec is dictating that we encode the values in Javascript, and we cannot use '+' to represent spaces due to the afore mentioned restriction
elspiko
How about using %20 for spaces?
Forgotten Semicolon
A: 

In that case, try the escape/unescape functions

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

Sergio
A: 

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("  ", " -");
Kelsey