java string 删除字符,从Java中的String中删除字符

"在Java中,字符串是不可变的,因此需要创建新的字符串来实现修改。问题在于尝试移除文件名中的.xml扩展名。解决方法是直接赋值给原字符串,使其指向替换后的新字符串:`id = id.replace(".xml", "");`"

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", "");

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值