Wednesday, March 28, 2012

Substring from URL

OK, i thought I was quite experienced with asp.net, though I have a silly question. I have a photo album on my website, but at the moment most of the data is stored in a database, and I get long URLs like www.arsluminis.com/photography/album/?aid=2, I'd like to have it like www.arsluminis.com/photography/album/1/2/, so I need to get the "1" and the "2" both in a parameter, I tried it using "indexOf" and "lastIndexOf", but I can't find it.
Anyone got an idea please?

Regards,
NielsHello,
if all the urls that you have are in the form of the url that you gave :
www.arsluminis.com/photography/album/?aid=2

then, you might have something like this:

string s = url.IndexOf('=');
then you can copy the value after "=" by using any method in Strings

then, also, use the methods of String like concat and create the new url that yoy want, in the format that you want.

best of luck
Consider using the .net class System.Uri. Pass the URL into the constructor and use its Query parameter to get a string representing the querystring.

If you want to look through individual parameters, there are two ways:
1. Brute force. Use the Split('&') method on the string class to create an array of elements. Then loop through them.
2. Regular expression. I think the expression is:
Parameter name;
=
any text
? OR end of line

Here's the reference I like for building regular expressions:
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136

The .net class is System.Text.RegularExpressions.Regex.

0 comments:

Post a Comment