This question already has answers here:
Does C# have an equivalent to JavaScript's encodeURIComponent()?
(7 answers)
UrlEncode - Javascript vs. C# [duplicate]
(5 answers)
Closed 9 years ago.
C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So
"http://www.example.com/my cool page"
would be encoded to
"http://www.example.com/my%20cool%20page"
Is there something baked into .NET to do this? Or is a regex my best bet?
Try
Uri.EscapeUriString("http://www.mysite.com/my cool page")
Try
Server.URLEncode(uri.ToString)
Try this:
HttpUtility.UrlEncode(String)
For example:
var url_encoded_string = HttpUtility.UrlEncode(userInput);
You could use System.Net.WebUtility.UrlEncode(string value).
Related
This question already has answers here:
Encoding parameters for a URL
(3 answers)
Closed 4 years ago.
I want to send search keywords with QueryString like google, atc...
and i need a function which convert text like this
Metallica nimes concert 2009 :)
to this:
Metallica+nimes+concert+2009+%3A%29
and retrieve it
Thanks and advance
you need to encode the string using:
HttpUtility.UrlEncode("Metallica nimes concert 2009 :)")
you'll need to reference to System.Web
This will work on simple strings, if you want to encode more characters see:
Server.UrlEncode vs. HttpUtility.UrlEncode. as Panagiotis Kanavos recommended
Trying to encode a string into a URL?
Why not use system.net.webutility.urlencode
Docs here
Rock here
This question already has answers here:
How to match numbers between X and Y with regexp?
(7 answers)
Closed 6 years ago.
I am new to learning Regex and I have tried almost everything myself and from the internet to find a Regex that accepts values from 0 to 65536 and yes I want to do it by Regex only. The Closest I got was 69999.
Here it is:
^(?:[0-5]?[0-9]{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6])$
See demo
Split it into multiple ranges: 0-9, 10-9999, 10000-59999, 60000-64999, 65000-65499, 65500-65529, 65530-65536
^(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])$
This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 7 years ago.
I have a string that is the contents of a webpage in this format :
value=foo&value1=bar&value2=foobar
What I would like to know is, is there a built-in way to convert this into an object/list of object/whatever so that I can loop through or access the values by keyname
I know I can split on '&', and then split on '=', but I believe there is a built-in way to do this, I just do not know what it is.
The language I am working in is C#.
You can use HttpUtility.ParseQueryString.
Eg.
NameValueCollection values = HttpUtility.ParseQueryString("value=foo&value1=bar&value2=foobar");
This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 8 years ago.
What does "#" mean before a string?
I saw this notation with paths:
string myfolder = #"C:\Users\";
But also with normal strings.
It means it's a literal string, so won't treat \ as an escape character, for example. This page should help you understand it better.
This question already has answers here:
How to build a query string for a URL in C#?
(39 answers)
Closed 9 years ago.
I have to write this data in querystring:
http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you
I got an error because of that symbol '&' i used.
In c# you can use this:-
HttpUtility.UrlEncode("http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you");
HttpUtility is a part of System.Web and this will ensure an of the non permitted query string char are url Encoded.
Once you do this you will get something like this http%3a%2f%2flocalhost%3a1256%2f4.market.ph.local%2fWEP%2fAdd.cshtml%3fdata%3dme%2b%26%2byou
On the receiver just decode it back.
Use urlencode($yourstring) or if you are hard coding it, use %26 to represent the ampersand.