String and Path

Last post 07-02-2009 9:25 AM by jagjot. 3 replies.

Sort Posts:

  • String and Path

    07-02-2009, 9:15 AM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    Hello,

    I am trying to create a few String extensions that given a path, for example:

    "Contents/Image/FileName.jpg"

    It provides the Path, FileName and Extension.

    For example:

    String fullPath = "Contents/Image/FileName.jpg";

    1. String path = fullPath.GetPath();
        Would return "Contents/Image/"

    2. String filename = fullPath.GetFileName();
         Would return "FileName"

    3. String extension = fullPath.GetExtension();
         Would return "jpg"

    My problem is the string part not the extension creating.

    How can I do this? And should I use Regex?

    Thanks,
    Miguel

  • Re: String and Path

    07-02-2009, 9:22 AM
    Answer
    • Contributor
      5,138 point Contributor
    • papabear
    • Member since 08-08-2005, 3:49 PM
    • Posts 851

    You could use Regular Expressions - they are pretty slick. Here is a nice quick look at using Named Capturing Groups: http://www.regular-expressions.info/named.html

    But it might be a lot simpler simply to use LastIndexOf

    string path = fullPath.subString(0, fullPath.LastIndexOf('/));

    etc...

    ;)

    give me suggestions for what to blog... http://www.myfriedmind.com/techblog -> thx

    Mark as "Answered" if this solves that wee old problem...
  • Re: String and Path

    07-02-2009, 9:23 AM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    Or just use:

    Path.GetPathRoot()
    Path.GetFileName()
    Path.GetExtension()

    I forgot about it ...

    Thanks,

    Miguel

  • Re: String and Path

    07-02-2009, 9:25 AM
    • Participant
      1,399 point Participant
    • jagjot
    • Member since 10-19-2006, 9:02 AM
    • United Kingdom
    • Posts 749

     If you mean your string is not creating file type extension. ?

    Then you you need to define fileType as well

    image/jpeg
    image/jpg
    image/jpe_
    image/pjpeg

    Hope its helpful..

    Jagjot Singh
    MCP, CCNA
Page 1 of 1 (4 items)