I am writing a file browser program that displays file directory path while a user navigates between folders/files.
I have the following String as file path:
"Files > Cold Storage > C > Capital"
I am using Java indexOf(String)
method to return the index of 'C'
character between > C >
, but it returns the first occurrence for it from this word Cold
.
I need to get the 'C'
alone which sets between > C >
.
This is my code:
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());
}
I need flexible solution that fits other proper problems Any help is appreciated!
A better approach would be to use a
List
ofString
s.