-1

I am trying to replace a hash char in a string but the following is not working the

 string address = "Blk 344, Jurong West, Street 11, #02-111";
 address.Replace("#","%23");

Any ideas guys been driving me crazy

Query String full

http://localhost:54965/SKATEZ/thankyou.aspx?firstname=Fiora&lastname=Ray&address=Blk%20344,%20Jurong%20West,%20Street%2011,%20#02-111&total=22&nirc=S6799954H&country=Singapore&orderid=85&postalcode=746112

I construct the url as follows

string url = "thankyou.aspx?firstname=" + firstname + "&" + "lastname=" + lastname + "&" + "address=" + HttpUtility.EscapeDataString(address) + "&" + "total=" + total + "&" + "nirc=" + tbID.Text + "&" + "country=" + ddlCountry.SelectedValue + "&" + "orderid=" + orderid + "&" + "postalcode=" + tbPostalCode.Text;
Response.Redirect(url);
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • You have to do something with the return value of `Replace`, like assign it back to the original string? – Rufus L Apr 17 '15 at 21:34
  • possible duplicate of [C# string replace does not work](http://stackoverflow.com/questions/13277667/c-sharp-string-replace-does-not-work) – chue x Apr 17 '15 at 21:34
  • It looks like you are trying to turn your string into something usable as a url parameter - in that case rather use [System.Uri.EscapeUriString() or System.Uri.EscapeDataString() or HttpUtility.UrlEncode()](http://stackoverflow.com/q/4396598/205233) – Filburt Apr 17 '15 at 21:36
  • msdn says, "Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string." – nur Apr 17 '15 at 21:38
  • @Filburt i tried that but cause the hash is right beside a number its not linking that any other ideas – c-sharp-and-swiftui-devni Apr 17 '15 at 22:13
  • @Filburt this is the whole url string after using escape its still ignoring the hash so it is it needs to be encoded ffs http://localhost:54965/SKATEZ/thankyou.aspx?firstname=Fiora&lastname=Ray&address=Blk%20344,%20Jurong%20West,%20Street%2011,%20#02-111&total=22&nirc=S6799954H&country=Singapore&orderid=85&postalcode=746112 – c-sharp-and-swiftui-devni Apr 17 '15 at 22:17
  • @Filburt i showed the url string in edit their please see why its still not escaping – c-sharp-and-swiftui-devni Apr 17 '15 at 22:18
  • Are you sure you're using System.Uri.EscapeDataString? It looks like you are using System.Uri.EscapeUriString which will fail on # - sorry that I did put them together in my first comment. – Filburt Apr 17 '15 at 22:35
  • @Filburt no look at the code im using "address=" + HttpUtility.EscapeDataString(address) + – c-sharp-and-swiftui-devni Apr 17 '15 at 22:37
  • As AlexD already mentioned: System.Web.HttpUtility doesn't offer this method - you'll have to show us the reference your HttpUtility class is from. – Filburt Apr 17 '15 at 22:49

2 Answers2

8

Try

address = address.Replace("#","%23");

Strings in C# are immutable:

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.

AlexD
  • 32,156
  • 3
  • 71
  • 65
0

Using System.Uri.EscapeDataString(string) should fix your issue:

var urlbuilder = new StringBuilder();

urlbuilder.AppendFormat("thankyou.aspx?firstname={0}", firstname);
urlbuilder.AppendFormat("&lastname={0}", lastname);
urlbuilder.AppendFormat("&address={0}", System.Uri.EscapeDataString(address));
urlbuilder.AppendFormat("&total={0}", total);
urlbuilder.AppendFormat("&nirc={0}", tbID.Text);
urlbuilder.AppendFormat("&country={0}", ddlCountry.SelectedValue);
urlbuilder.AppendFormat("&orderid={0}", orderid);
urlbuilder.AppendFormat("&postalcode={0}", tbPostalCode.Text);

Response.Redirect(urlbuilder.ToString());

(using System.Text.StringBuilder to compose your url makes the code a little more readable)

Filburt
  • 17,626
  • 12
  • 64
  • 115