Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

Get part of a string

Hi there

I have this string:

http://testing.com/testing/file.aspx?id=1&fn=docfile.doc&ll=333

how to I get docfile.doc into a string, please note, this filename can be any length or with any extension.

also, where I have &fn=, it could be ?fn=


Thankyou kindly
Avatar of infochandru
infochandru
Flag of India image

use,
Request.QueryString["fn"].ToString();
Avatar of mousemat24
mousemat24

ASKER

sorry, I forgot, the value is stored in the db, so I cant do a QueryString, have to use some logic
This can aid you..,

string fileName = Request.QueryString["fn"].ToString();
try this
string file = "";

if (Request.QueryString["fn"] != null){
  file = Request.QueryString["fn"].ToString();
}
To prevent error when QueryString do not passed
try

substring(str,st.indexOf('&fn=')+1,st.indexOf('&')-st.indexOf('&fn=')+1)
You can get the value from the URL, only through QueryString. else you can pass value between pages by Session. But the value won't be visible in URL in this case..
Avatar of Reza Rad
string[] strarray="http://testing.com/testing/file.aspx?id=1&fn=docfile.doc&ll=333".Split("&");
foreach (string stritem in strarray)
{
if(stritem.StartsWith("fn=")
{
stritem=stritem.Substring(3,stritem.Lenght-3);
break;
}
}

result is stritem
ASKER CERTIFIED SOLUTION
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
reza_rad

What happens if it was ?fn=

you code wouldnt work would it?

Thanks
and also, the extension name could be myfile.aasdsddd

So I think your code would not work
just you need to change first line,
try this now:

string[] strarray = "http://testing.com/testing/file.aspx?id=1&fn=docfile.doc&ll=333".Split(new string[]{"&","?"},StringSplitOptions.None);
            string strresult = string.Empty;
            foreach (string stritem in strarray)
            {
                if (stritem.StartsWith("fn="))
                {
                    strresult = stritem.Substring(3, stritem.Length - 3);
                    break;
                }
            }

Open in new window

in previous code strresult is the result you want, just test it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will find whatever occurs between "fn=" and "&" 
maybe fn=anyfile.doc was the last thing in querystring. so there will be no ending "&" !

thankyou guys for helping me!! really kind of you
reza_rad is correct, change the regular expression in my previous code snippet to:

.*fn=(.*\..*)(&.*|\s$)

To find everything between "fn=" and either "&" or the end of the string.

did you test @tgerbert 's code? there is a bug in this code which i concern about! if your fn= ... text ends with nothing this code will return nothing. this was not correct, but you select it as assisted solution!
by the way glad to solve at all
be happy
@tgerbert:
that's right now :-)
regards,

sorry man, I hope its ok?
It's OK.
no problem:-)