Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 09, 2008 08:01 AM by stmarti
May 06, 2008 08:17 PM|LINK
I need to traverse through my string. The first time around I want to grab the last folder. So for example
I need to grab "/folder2/" and I'm not sure how to go about this using SubString.
The second time around and so fourth I need to move backwards. So I need to in my for loop then grab "/folder1/" the next time around. The string can have x number of folders in it.
May 06, 2008 08:25 PM|LINK
I would use the string.Split function and then use a regular for loop counting backwards.
May 07, 2008 12:51 PM|LINK
You can have a look at this article. It shows you how to search files in directories and subdirectories recursively. You said you wanted them in reverse order. You can apply some logic, say create an arraylist and add the directory names in reverse order
or something similar. http://support.microsoft.com/kb/303974
May 08, 2008 06:40 AM|LINK
Provided you only have the path part of a Uri (and Uri,GetLeftPart(UriPartial.Path) can help here), you can use System.IO.Path.GetDirectoryName() and .GetFileName.
GetDirectoryName returns all before the last separator, GetFileName returns all after the last separator. All methods of System.IO.Path handle both / and \ (as does Win32 under the covers in most cases).
url = url.Substring(0, indexPos2) +
lastFolder = url.Substring(indexPos1 + 1, indexPos2 - indexPos1 - 1);
lastFolder is the value whoch contains the FolderName.
May 08, 2008 07:08 AM|LINK
First find the last index of / in the sring and the cut the substring to lastindex-1 length then again find the last index of / and then cut the string from lastindex + 1 till end to get the name of the folder...........
May 09, 2008 08:01 AM|LINK
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 );