3

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.

7 Answers 7

18

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

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

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

1 Comment

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.
1

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

Comments

1

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 :)

3 Comments

Jon, this has a bug if there is no trailing slash.
trialing slash? I meant second slash.
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).
1

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.

Comments

0
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.

1 Comment

Agreed. Although that wasn't the question.
0

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.

Comments

0
folderName.Split('/')[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.