1

blaBla\John.Boy

string _uName = id.Name.Split('\\')[1];
string _userName = _uName.Replace('.', ' ');

will return: "John Boy"

I want to use the replace, but with a replaceAll.

I have string url="Account/WindowsLogin.aspx?ReturnUrl=%2fMemberPages%2fcustomerDataStorePortal.aspx"

from this I want to create string NewUrl="/MemberPages/customerDataStorePortal.aspx"

so get data after the '=' and replace '%2F' with '/'

so far:

string redirectUrl1 = redirectUrlOLD.Split('=')[1];
string redirectUrl = redirectUrl1.Replace('%2F', '/');

is flagging %2F as too many characters

John
  • 3,853
  • 20
  • 69
  • 146

5 Answers5

5

"" is for a string

'' represents a single char

This is the way that you want to go

redirectUrl1.Replace("%2F", "/");
David Pilkington
  • 13,297
  • 3
  • 39
  • 67
1
string redirectUrl = redirectUrl1.Replace("%2F", "/");

Use " instead '

Davin Tryon
  • 64,963
  • 15
  • 144
  • 131
user1924375
  • 10,091
  • 6
  • 19
  • 27
1

Use

Uri.UnescapeDataString("Account/WindowsLogin.aspx?ReturnUrl=%2fMemberPages%2fcustomerDataStorePortal.aspx");

Also see the following link.

Community
  • 1
  • 1
Mez
  • 4,511
  • 4
  • 26
  • 55
1

You could use the Uri class and it's UnescapeDataString method:

string returnUrlParam = "?ReturnUrl=";
int paramIndex = url.IndexOf(returnUrlParam);
if (paramIndex >= 0)
{
    string param = url.Substring(paramIndex + returnUrlParam.Length);
    string newUrl = Uri.UnescapeDataString(param); // "/MemberPages/customerDataStorePortal.aspx"
}

If you can add a reference to System.Web you can use System.Web.HttpUtility:

string queryString = url.Substring(url.IndexOf('?') + 1);
var queryParams = System.Web.HttpUtility.ParseQueryString(queryString);
string newUrl = queryParams["ReturnUrl"]; // "/MemberPages/customerDataStorePortal.aspx"

Note that you cannot add System.Web if your target-framework is: framework x client profile(default for winforms, wpf etc.). You need to select the full framework.

Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
0

Along with using a string replacement instead of a char replacement, you can chain your methods together. Also it looks like you're replacing an uppercase F when the string contains lowercase f

string redirectUrl = url.Split('=')[1].Replace("%2f", "/"); 
Jonesopolis
  • 24,241
  • 10
  • 66
  • 110