I need to traverse through my string. The first time around I want to grab the last folder. So for example
"/folder1/folder2/somepage.aspx"
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.
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
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).
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...........
dba123
Contributor
2726 Points
1364 Posts
Grab the last value in string
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
"/folder1/folder2/somepage.aspx"
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.
huenemeca
Participant
835 Points
181 Posts
Re: Grab the last value in string
May 06, 2008 08:25 PM|LINK
I would use the string.Split function and then use a regular for loop counting backwards.
http://www.BlogOfSeparation.com
bullpit
All-Star
21838 Points
4822 Posts
Re: Grab the last value in string
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
Max
Let Me Google That For You!
rjcox
Contributor
7064 Points
1444 Posts
Re: Grab the last value in string
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).
So
Path.GetFileName(Path.GetDirectoryName("/folder1/folder2/somepage.aspx"))returns "folder2".Bhanu241177
Member
7 Points
4 Posts
Re: Grab the last value in string
May 08, 2008 06:40 AM|LINK
url=
"/folder1/folder2/somepage.aspx"; indexPos2 = url.LastIndexOf("/");url = url.Substring(0, indexPos2) +
"%" + url.Substring(indexPos2 + 1, (url.Length - indexPos2 - 1)); indexPos1=url.LastIndexOf("/");lastFolder = url.Substring(indexPos1 + 1, indexPos2 - indexPos1 - 1);
lastFolder is the value whoch contains the FolderName.
vijaymann1
Member
208 Points
46 Posts
Re: Grab the last value in string
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...........
Thanks
Software Developer Noida India
stmarti
Contributor
5083 Points
1061 Posts
Re: Grab the last value in string
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 );
- ...