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,965
  • 21
  • 77
  • 163

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,528
  • 3
  • 41
  • 73
1
string redirectUrl = redirectUrl1.Replace("%2F", "/");

Use " instead '

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
user1924375
  • 10,581
  • 6
  • 20
  • 27
1

Use

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

Also see the following link.

Community
  • 1
  • 1
Mez
  • 4,666
  • 4
  • 29
  • 57
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
  • 450,073
  • 74
  • 686
  • 939
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
  • 25,034
  • 12
  • 68
  • 112