Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I use Substring in this context?

    I have a following path name which varies all the time. Say for example the path name may be like these:

    /APC101/image2.gif
    /CE2_OPEC/image2_s.gif
    /EF0OP_12/image3Sm.gif

    In the above the first ones are directory names which varies in the code.

    I have to remove the first directory and concatenate the image name with another string. So in this case I have to remove from '/' to '/' including the '/'

    How can I use substring function to do this? Otherwise is there any other function I can use? I have to incorporate this in C# later.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by chelvis View Post
    I have a following path name which varies all the time. Say for example the path name may be like these:

    /APC101/image2.gif
    /CE2_OPEC/image2_s.gif
    /EF0OP_12/image3Sm.gif

    In the above the first ones are directory names which varies in the code.

    I have to remove the first directory and concatenate the image name with another string. So in this case I have to remove from '/' to '/' including the '/'

    How can I use substring function to do this? Otherwise is there any other function I can use? I have to incorporate this in C# later.
    You can use .substring once you get the offset of the last '/', using .lastIndexOf.

    Code:
    <SCRIPT type='text/javascript'>
    String.prototype.replacePath=function(newPath)
    {
     return newPath+this.replace(/^.*\/([^\/]*)$/, "$1");
    }
    
    alert("/EF0OP_12/image3Sm.gif".replacePath('/my/new/path/'));
    </SCRIPT>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •