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
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
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() ;
string fileName = Request.QueryString["fn"].
try this
string file = "";
if (Request.QueryString["fn"] != null){
file = Request.QueryString["fn"]. ToString() ;
}
To prevent error when QueryString do not passed
string file = "";
if (Request.QueryString["fn"]
file = Request.QueryString["fn"].
}
To prevent error when QueryString do not passed
try
substring(str,st.indexOf(' &fn=')+1,s t.indexOf( '&')-st.in dexOf('&fn =')+1)
substring(str,st.indexOf('
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..
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
foreach (string stritem in strarray)
{
if(stritem.StartsWith("fn=
{
stritem=stritem.Substring(
break;
}
}
result is stritem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
reza_rad
What happens if it was ?fn=
you code wouldnt work would it?
Thanks
What happens if it was ?fn=
you code wouldnt work would it?
Thanks
ASKER
and also, the extension name could be myfile.aasdsddd
So I think your code would not work
So I think your code would not work
just you need to change first line,
try this now:
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;
}
}
in previous code strresult is the result you want, just test it
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This will find whatever occurs between "fn=" and "&"
maybe fn=anyfile.doc was the last thing in querystring. so there will be no ending "&" !
maybe fn=anyfile.doc was the last thing in querystring. so there will be no ending "&" !
ASKER
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.
.*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,
that's right now :-)
regards,
ASKER
sorry man, I hope its ok?
It's OK.
no problem:-)
no problem:-)
Request.QueryString["fn"].