?
Solved

Get part of a string

Posted on 2010-01-07
20
Medium Priority
?
222 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:mousemat24
  • 8
  • 5
  • 2
  • +4
20 Comments
 
LVL 3

Expert Comment

by:infochandru
ID: 26199455
use,
Request.QueryString["fn"].ToString();
0
 

Author Comment

by:mousemat24
ID: 26199473
sorry, I forgot, the value is stored in the db, so I cant do a QueryString, have to use some logic
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 26199477
This can aid you..,

string fileName = Request.QueryString["fn"].ToString();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:fsouzabrasil
ID: 26199481
try this
string file = "";

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

Expert Comment

by:rajvja
ID: 26199485
try

substring(str,st.indexOf('&fn=')+1,st.indexOf('&')-st.indexOf('&fn=')+1)
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 26199492
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199507
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
 
LVL 30

Accepted Solution

by:
Reza Rad earned 1000 total points
ID: 26199514
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
 

Author Comment

by:mousemat24
ID: 26199529
reza_rad

What happens if it was ?fn=

you code wouldnt work would it?

Thanks
0
 

Author Comment

by:mousemat24
ID: 26199549
and also, the extension name could be myfile.aasdsddd

So I think your code would not work
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199645
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199649
in previous code strresult is the result you want, just test it
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 1000 total points
ID: 26199815
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199857
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
 

Author Comment

by:mousemat24
ID: 26199863
thankyou guys for helping me!! really kind of you
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26199886
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199906

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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26199913
@tgerbert:
that's right now :-)
regards,

0
 

Author Comment

by:mousemat24
ID: 26200703
sorry man, I hope its ok?
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26207354
It's OK.
no problem:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question