c# - How to Remove number from Extension with string? -
i want remove number extension string in c#.
for example : "url1234.pdf" want last answer looks "url.pdf"
thank contribution
var filename = ""; if (file != null && file.contentlength > 0) { filename = system.io.path.getfilenamewithoutextension(file.filename); //path.getfilename(file.filename); var extension = system.io.path.getextension(file.filename); dbconnect.opendb(); dbconnect.dbselect("select max(id) id tblfileupload"); if(dbconnect.dr.read()) { filename += dbconnect.dr["id"].tostring(); } dbconnect.closedb(); var path = path.combine(server.mappath("~/file/"), filename+extension); new fileuploadlayer().save("aa", filename, file.contentlength); file.saveas(path); uploadfile("aa"); }
i save file extension(.pdf). file name has numbers also.(url1234.pdf).so, when call need remove numbers , need string part (url.pdf).
you can use regex shown or simple linq query, i'd recommend system.io.path
:
string originalpath = "url1234.pdf"; string dir = path.getdirectoryname(originalpath); // in case "" string extension = path.getextension(originalpath); // .pdf string fn = path.getfilenamewithoutextension(originalpath); // url1234 string newfn = string.concat(fn.where(c => !char.isdigit(c))); // url string newpath = path.combine(dir, newfn + extension); // url.pdf
Comments
Post a Comment