tags:

views:

3968

answers:

7

Hi, I want to extract the first folder in the URL below, in this example it is called 'extractThisFolderName' but the folder could have any name and be any length. With this in mind how can I use substring to extract the first folder name?

The string: www.somewebsite.com/extractThisFolderName/leave/this/behind

String folderName = path.Substring(path.IndexOf(@"/"),XXXXXXXXXXX);

It's the length I'm struggling with.

Thanks all

+16  A: 

If you're getting a Uri, why not just do uri.Segments[0]?

Or even path.Split(new Char[] { '/' })[1] ?

Daniel Schaffer
+1 Substring is a poor choice for this.
Rob Allen
It's only a poor choice if you're going to be using more than one part of the string. Otherwise, it's demonstrably faster to scan for the first two slashes, and ignore the rest of the string.
NilObject
A: 
int start = path.IndexOf('/');
int end = path.IndexOf('/', start + 1);
if (end == -1) end = path.Length;
string folderName = path.Substring(start + 1, end - start - 1);

EDIT: Daniel Schaffer's answer about using uri segments is preferable, but left this in as it may be your path is not really a valid uri.

Sam Meldrum
this won't work if the URL starts with http://
Sebastian
Agreed. Although that wasn't the question.
Sam Meldrum
A: 

If you're going to be using each path part, you can use:

String[] parts = path.Split('/');

At which point you can access the "extractThisFolderName" part by accessing parts[1].

Alternatively, you can do this to splice out the foldername:

int firstSlashIndex = path.IndexOf('/');
int secondSlashIndex = path.IndexOf('/', firstSlashIndex + 1);
String folderName = path.Substring(firstSlashIndex + 1, secondSlashIndex - firstSlashIndex);
NilObject
A: 

You could do:

string myStr = "www.somewebsite.com/extractThisFolderName/leave/this/behind";
int startIndex = myStr.IndexOf('/') + 1;
int length = myStr.IndexOf('/', startIndex) - startIndex;
Console.WriteLine(myStr.Substring(startIndex, length));

At the same point I assume this is being done in ASP.Net if so I think there might be another way to get this without doign the querying.

JoshBerke
A: 

Daniel's answer gives you other practical ways of doing it. Another alternative using substring:

int start = path.IndexOf('/')+1; // Note that you don't need a verbatim string literal
int secondSlash = path.IndexOf('/', start);
return path.Substring(start, secondSlash-start);

You'll want to add some error checking in there, of course :)

Jon Skeet
Jon, this has a bug if there is no trailing slash.
Sam Meldrum
trialing slash? I meant second slash.
Sam Meldrum
That's where the "you'll want to add some error checking in there, of course" comes in - checking for -1 as the return for both calls to IndexOf (which would make start=0 of course).
Jon Skeet
Missed that - sorry!
Sam Meldrum
A: 
folderName.Split('/')[1]
devio
+1  A: 

The problem also lends itself to regular expressions. An expression like:

(?<host>.*?)/(?<folder>.*?)/

Is clear about what's going on and you can get the data out by those names.

PEZ