我有一个字符串,它可以采用两种形式
第一种形式
 file:///mnt/sdcard/myfolder/myfile.txt第二种形式
 /file://mnt/sdcard/myfolder/myfile.txt并且我需要字符串始终以
/mnt/sdcard/myfolder/myfile.txt因此,我使用了replace命令
myPath=path.replace("file://", "");
myPath= path.replace("/file:/", "");但不幸的是它不能工作
和字符串myPath结果
file:///mnt/sdcard/myfolder/myfile.txt怎么啦?
发布于 2012-10-06 01:09:07
如果你保证你的字符串总是这两种形式中的一种,为什么不直接使用
myPath = path.substring(path.indexOf("/mnt"));如果你不确定你的路径包含"/mnt",你可以尝试这样做:
if (path.contains("file:"))
{
    myPath = path.substring(path.indexOf(":/") + 1);
    while (myPath.startsWith("//"))
        myPath = myPath.substring(1);
}发布于 2012-10-06 01:07:37
尝试:
path=path.replace("file://", "");
myPath= path.replace("/file:/", "");发布于 2012-10-06 01:09:16
使用第二个调用覆盖第一个myPath = path.replace()调用,该调用可能不会替换任何内容,并返回整个字符串。
https://stackoverflow.com/questions/12750879
复制相似问题