简体   繁体   中英

How to change .txt file extension to .xml file extension using regular expression?

String FileName= vars.get(''resultspath" + "/" + vars.get("path") + "/" + props.get("") +"/" + Vars.get("Name")

输出文件的格式为“ Name.txt”,我希望将其更改为“ Name.xml”,如何使用正则表达式进行更改。

You can use split, assuming file name doesn't contain '.'

String s ="Name.txt";
s = s.split("\\.")[0]+".xml";

Another, solution could be using replaceAll , in this case, it doesn't matter whether filename contains '.' or not.

s = s.replaceAll("\\.txt$",".xml");

You can use something like String changedName = fileName.replaceAll(".txt$", ".xml"); That way, .txt in the end of file name will be changed to .xml (Note $ after .txt ), and it works even if the file name contains . somewhere else

In JavaScript it would be something like this:

"Name.txt".replace(/\.txt$/, '.xml');

In Java, you can do it like this:

"Name.txt".replaceFirst("\\.txt$", ".xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM