我正在编写一个文件浏览器程序,该程序在用户在文件夹/文件之间导航时显示文件目录路径。

我有以下字符串作为文件路径:

"Files > Cold Storage > C > Capital"


我正在使用Java indexOf(String)方法返回'C'之间> C >字符的索引,但是它从单词Cold返回它的第一个匹配项。

我需要单独设置在'C'之间的> C >
这是我的代码:

StringBuilder mDirectoryPath = new StringBuilder("Files > Cold Storage > C > Capital");
String mTreeLevel = "C";
int i = mDirectoryPath.indexOf(mTreeLevel);
if (i != -1) {
    mDirectoryPath.delete(i, i + mTreeLevel.length());
}


我需要适合其他适当问题的灵活解决方案
任何帮助表示赞赏!

最佳答案

更好的方法是使用ListString

public void test() {
    List<String> directoryPath = Arrays.asList("Files", "Cold Storage", "C", "Capital");
    int cDepth = directoryPath.indexOf("C");
    System.out.println("cDepth = " + cDepth);
}

关于java - Java:返回错误字符的IndexOf(String string),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31022746/

10-14 12:30
查看更多