2

I am trying find a string in below string.

http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779

by using http://example.com/TIGS/SIM/Lists string. How can I get Team Discussion word from it?

Some times strings will be

   http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779
     I need `Team Discussion`

http://example.com/TIGS/ALIF/Lists/Artifical Lift Discussion Forum 2/DispForm.aspx?ID=8

    I need  `Artifical Lift Discussion Forum 2`
1
  • 1
    A bit of different approach; load it into a Uri and use the Parts property. Commented Oct 12, 2011 at 17:55

5 Answers 5

2

If you're always following that pattern, I recommend @Justin's answer. However, if you want a more robust method, you can always couple the System.Uri and Path.GetDirectoryName methods, then perform a String.Split. Like this example:

String url = @"http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779";
System.Uri uri = new System.Uri(url);
String dir = Path.GetDirectoryName(uri.AbsolutePath);
String[] parts = dir.Split(new[]{ Path.DirectorySeparatorChar });
Console.WriteLine(parts[parts.Length - 1]);

The only major problem, however, is you're going to wind up with a path that's been "encoded" (i.e. your space is now going to be represented by a %20)

Sign up to request clarification or add additional context in comments.

Comments

1

This solution will get you the last directory of your URL regardless of how many directories are in your URL.

string[] arr = s.Split('/');
string lastPart = arr[arr.Length - 2];

You could combine this solution into one line, however it would require splitting the string twice, once for the values, the second for the length.

Comments

0

If you wanted to see a regular expression example:

        string input = "http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779";
        string given = "http://example.com/TIGS/SIM/Lists";
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(given + @"\/(.+)\/");
        System.Text.RegularExpressions.Match match = regex.Match(input);
        Console.WriteLine(match.Groups[1]); // Team Discussion

Comments

0

Here's a simple approach, assuming that your URL always has the same number of slashes before the are you want:

var value = url.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)[5];

Comments

0

Here is another solution that provides the following advantages:

  • Does not require the use of regular expressions.
  • Does not require a certain 'count' of slashes be present (indexing based of a specific number). I consider this a key benefit because it makes the code less likely to fail if some part of the URL changes. Ultimately it is best to base your parsing logic off which part of the text's structure you consider least likely to change.

This method, however, DOES rely on the following assumptions, which I consider to be the least likely to change:

  • URL must have "/Lists/" right before target text.
  • URL must have "/" right after target text.

Basically, I just split the string twice, using text that I expect to be surrounding the area I am interested in.

String urlToSearch = "http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx";
String result = "";

// First, get everthing after "/Lists/"
string[] temp1 = urlToSearch.Split(new String[] { "/Lists/" }, StringSplitOptions.RemoveEmptyEntries);                
if (temp1.Length > 1)
{
    // Next, get everything before the first "/"
    string[] temp2 = temp1[1].Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
    result = temp2[0];
}

Your answer will then be stored in the 'result' variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.