Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在java中读取一个字符串,它有类似的东西
xyz.rpt abcde.img erhteds.doc aqwer.rpt
并修改其中包含的字符串.rpt。如果字符串.rpt末尾有我需要将其更改为.doc
.rpt
.doc
OUTPUT: xyz.doc abcde.img erhteds.doc aqwer.doc
如果您逐行阅读,以下代码应将最后一次出现的“.rpt”替换为“.doc”
StringBuilder b = new StringBuilder(yourString); b.replace(yourString.lastIndexOf(".rpt"), yourString.lastIndexOf(".rpt") + 1, ".doc" ); yourString = b.toString();
注意如果字符串不包含“.rpt”,这将引发异常