You can use regular expressions for this also:
string test = "/folder1/folder2/somepage.aspx";
System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex( "[^/]+" );
System.Text.RegularExpressions.Match result = pattern.Match( test );
while( result.Success )
{
Response.Write( result.Value + "<br />" );
result = result.NextMatch( );
}
You can fine tune this:
- get the first folder, use this pattern "^[^/]+"
- get the last folder, use this pattern "[^/]+$"
- get the folders from right top left: System.Text.RegularExpressions.Regex( "[^/]+", System.Text.RegularExpressions.RegexOptions.RightToLeft );
- ...