Grab the last value in string

Last post 05-09-2008 4:01 AM by stmarti. 6 replies.

Sort Posts:

  • Grab the last value in string

    05-06-2008, 4:17 PM
    • Loading...
    • dba123
    • Joined on 12-13-2003, 12:04 AM
    • Posts 986

    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.
     

    C# Web Developer

    When is Microsoft going to get rid of VB.NET!
  • Re: Grab the last value in string

    05-06-2008, 4:25 PM
    • Loading...
    • huenemeca
    • Joined on 08-02-2002, 2:19 PM
    • Posts 124

     I would use the string.Split function and then use a regular for loop counting backwards.

  • Re: Grab the last value in string

    05-07-2008, 8:51 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    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

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: Grab the last value in string

    05-08-2008, 2:40 AM
    • Loading...
    • rjcox
    • Joined on 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 872

    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".
    Richard
  • Re: Grab the last value in string

    05-08-2008, 2:40 AM
    • Loading...
    • Bhanu241177
    • Joined on 05-23-2005, 4:41 AM
    • Posts 4

    Here is the code snippet to code the LastFolder of the URI: 

    string url = string.Empty;

    string lastFolder = string.Empty;

    int indexPos1,indexPos2;

    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.

  • Re: Grab the last value in string

    05-08-2008, 3:08 AM
    • Loading...
    • vijaymann1
    • Joined on 02-27-2008, 10:06 AM
    • delhi
    • Posts 44

    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

    Vijay Mann
    Software Developer Noida India
  • Re: Grab the last value in string

    05-09-2008, 4:01 AM
    Answer
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 542

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

    - ... 

Page 1 of 1 (7 items)