I am trying to remove the .xml part of a file name with the following code:
String id = fileR.getName();
id.replace(".xml", "");
idList.add(id);
The problem is that it is not removing it and I have no clue why it won't remove the target text.
EDIT: Actually I realize that the replace function won't find the .xml, so I guess the question is, how do I get rid of those last 4 characters?
Here is the string that is being passed in:
0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e.xml
Thanks,
解决方案
Strings in java are immutable. That means you need to create a new string or overwrite your old string to achieve the desired affect:
id = id.replace(".xml", "");
"在Java中,字符串是不可变的,因此需要创建新的字符串来实现修改。问题在于尝试移除文件名中的.xml扩展名。解决方法是直接赋值给原字符串,使其指向替换后的新字符串:`id = id.replace(".xml", "");`"

被折叠的 条评论
为什么被折叠?



