Home > database >  Nodejs splitting string
Nodejs splitting string

Time:10-12

let NewName= params.strdocumentname   "("   results.rows[0].arrayfilecount  ")";
strdocumentname = params.strdocumentname = NewName;

the current output is Jayson.png(2)

desired output Jayson (2).png

How do i do that using the code above?

CodePudding user response:

Use the following code:-

const path = require('path');
const extension= path.extname(params.strdocumentname);
const name= params.strdocumentname.replace(extension,"("   results.rows[0].arrayfilecount  ")");
const newName = name   extension;

Hope it helps Thanks

CodePudding user response:

I think your params.strdocumentname contains the extension of the file also. Try this code;

let [fileName, extension] = params.strdocumentname.split('.');
let NewName= fileName   "("   results.rows[0].arrayfilecount  ")."   extension;
strdocumentname = params.strdocumentname = NewName;

see working fiddle

let mockFileName = "abc.png"
let mockArrayCount = '1'

let [fileName, extension] = mockFileName.split('.');
let NewName= fileName   "("   mockArrayCount  ")."   extension;

console.log(NewName)
alert(NewName)

  • Related