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

S

SM

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
 
L

Lee

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?


--
 
S

SM

SM said:











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
 
S

scripts.contact

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

SM

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top