A simple 'Replace' and 'Substring' question: How to add a string to another string

Discussion in 'Javascript' started by SM, Apr 25, 2007.

  1. SM

    SM Guest

    Hello,
    I have a simple question, but can't find the answer.
    I have a string that contains a path to a file
    I want to add another string to the end of that string

    So, if i have :
    path = document/disco/album/hello.doc

    i want it to become :
    document/disco/album/hello_large.xls

    it doesnt necessary means that the file will always end with .doc it
    could be also .doc, xls... so i dont want to hardcode that part.

    I've try using replace and substring with no success

    Can someone help me with such a simple function?

    Thanks
    Marco
     
    SM, Apr 25, 2007
    #1
    1. Advertising

  2. SM

    Lee Guest

    SM said:
    >
    >Hello,
    >I have a simple question, but can't find the answer.
    >I have a string that contains a path to a file
    >I want to add another string to the end of that string
    >
    >So, if i have :
    >path = document/disco/album/hello.doc
    >
    >i want it to become :
    >document/disco/album/hello_large.xls
    >
    >it doesnt necessary means that the file will always end with .doc it
    >could be also .doc, xls... so i dont want to hardcode that part.
    >
    >I've try using replace and substring with no success
    >
    >Can someone help me with such a simple function?


    The first step is to define precisely what you want.
    You don't want to add another string to the end of a string.
    It sounds like you're asking how to replace the last "dot" and
    whatever follows it with "_large.xls". Is that right, or could
    the replacement text be variable?


    --
     
    Lee, Apr 25, 2007
    #2
    1. Advertising

  3. SM

    SM Guest

    On Apr 25, 1:01 pm, Lee <> wrote:
    > SM said:
    >
    >
    >
    >
    >
    > >Hello,
    > >I have a simple question, but can't find the answer.
    > >I have a string that contains a path to a file
    > >I want to add another string to the end of that string

    >
    > >So, if i have :
    > >path = document/disco/album/hello.doc

    >
    > >i want it to become :
    > >document/disco/album/hello_large.xls

    >
    > >it doesnt necessary means that the file will always end with .doc it
    > >could be also .doc, xls... so i dont want to hardcode that part.

    >
    > >I've try using replace and substring with no success

    >
    > >Can someone help me with such a simple function?

    >
    > The first step is to define precisely what you want.
    > You don't want to add another string to the end of a string.
    > It sounds like you're asking how to replace the last "dot" and
    > whatever follows it with "_large.xls". Is that right, or could
    > the replacement text be variable?
    >
    > --


    Hey Lee,
    Sorry for not being so clear.
    The replacement text is always the text '_large', and the replacement
    always happens before the extension of the file (that is before the
    first 'dot'). But i do want to keep the extension.

    ie
    images/disco/alive.jpg ---> images/disco/alive_large.jpg

    images/disco/alive.jpg ---> images/disco/alive_large.jpg

    Hope this helps

    Thanks again
    Marco
     
    SM, Apr 27, 2007
    #3
  4. On Apr 26, 6:58 pm, SM <> wrote:
    > The replacement text is always the text '_large', and the replacement
    > always happens before the extension of the file (that is before the
    > first 'dot'). But i do want to keep the extension.
    >
    > ie
    > images/disco/alive.jpg ---> images/disco/alive_large.jpg
    >
    > images/disco/alive.jpg ---> images/disco/alive_large.jpg


    var newText=textVar.replace(/(.+)(\..+)/,'$1_large$2')

    OR

    var lastIx=textVar.lastIndexOf('.')
    var newText=textVar.slice(0,lastIx)+'_large'+textVar.slice(lastIx)
     
    scripts.contact, Apr 27, 2007
    #4
  5. SM

    SM Guest

    On Apr 27, 1:50 am, "scripts.contact" <>
    wrote:
    > On Apr 26, 6:58 pm, SM <> wrote:
    >
    > > The replacement text is always the text '_large', and the replacement
    > > always happens before the extension of the file (that is before the
    > > first 'dot'). But i do want to keep the extension.

    >
    > > ie
    > > images/disco/alive.jpg ---> images/disco/alive_large.jpg

    >
    > > images/disco/alive.jpg ---> images/disco/alive_large.jpg

    >
    > var newText=textVar.replace(/(.+)(\..+)/,'$1_large$2')
    >
    > OR
    >
    > var lastIx=textVar.lastIndexOf('.')
    > var newText=textVar.slice(0,lastIx)+'_large'+textVar.slice(lastIx)


    thank you

    Marco
     
    SM, Apr 27, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominique Deleris
    Replies:
    4
    Views:
    478
  2. Paul
    Replies:
    19
    Views:
    13,022
    Netocrat
    Nov 10, 2005
  3. Replies:
    7
    Views:
    454
    Robbie Hatley
    May 31, 2007
  4. magix

    replace substring

    magix, May 26, 2008, in forum: C Programming
    Replies:
    20
    Views:
    962
    Flash Gordon
    Jun 6, 2008
  5. Replies:
    3
    Views:
    212
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page