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
mousemat24Asked:
Who is Participating?
 
Reza RadConnect With a Mentor Consultant, TrainerCommented:
this one is correct

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;
}
}

Open in new window

0
 
infochandruCommented:
use,
Request.QueryString["fn"].ToString();
0
 
mousemat24Author Commented:
sorry, I forgot, the value is stored in the db, so I cant do a QueryString, have to use some logic
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
waltersnowslinarnoldCommented:
This can aid you..,

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

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

substring(str,st.indexOf('&fn=')+1,st.indexOf('&')-st.indexOf('&fn=')+1)
0
 
waltersnowslinarnoldCommented:
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..
0
 
Reza RadConsultant, TrainerCommented:
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
0
 
mousemat24Author Commented:
reza_rad

What happens if it was ?fn=

you code wouldnt work would it?

Thanks
0
 
mousemat24Author Commented:
and also, the extension name could be myfile.aasdsddd

So I think your code would not work
0
 
Reza RadConsultant, TrainerCommented:
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

0
 
Reza RadConsultant, TrainerCommented:
in previous code strresult is the result you want, just test it
0
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
You can use a RegEx object with a regular expression.  This will find whatever occurs between "fn=" and "&"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string testInput = "http://testing.com/testing/file.aspx?id=1&fn=docfile.doc&ll=333";
            string fileName = null;

            Regex re = new Regex(@".*fn=(.*\..*)&.*");

            if (re.IsMatch(testInput))
                fileName = re.Match(testInput).Groups[1].Value;

            if (fileName != null)
                Console.WriteLine("The file name is: {0}", fileName);
            else
                Console.WriteLine("Could not find file name in: {0}", testInput);

            Console.ReadKey();
        }
    }
}

Open in new window

0
 
Reza RadConsultant, TrainerCommented:
This will find whatever occurs between "fn=" and "&" 
maybe fn=anyfile.doc was the last thing in querystring. so there will be no ending "&" !

0
 
mousemat24Author Commented:
thankyou guys for helping me!! really kind of you
0
 
Todd GerbertIT ConsultantCommented:
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.
0
 
Reza RadConsultant, TrainerCommented:

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
0
 
Reza RadConsultant, TrainerCommented:
@tgerbert:
that's right now :-)
regards,

0
 
mousemat24Author Commented:
sorry man, I hope its ok?
0
 
Reza RadConsultant, TrainerCommented:
It's OK.
no problem:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.