我正在尝试使用字符串。就我而言,我有这个字符串:
String test ="/mnt/sdcard/Download/images.jpeg|/mnt/sdcard/Download/images.jpeg|/mnt/sdcard/Download/images-1.jpeg|/mnt/sdcard/Download/images-2.jpeg|";
此字符串包含我的 android 模拟器中的 4 个图像路径。如您所见,每条路径都由“|”字符分隔。我想做的很简单。我必须创建一个 for 循环,在其中找到没有“|”字符的每条路径,并且在每个循环上我需要删除创建的路径,但在这种情况下使用“|”字符。我已经实现了代码,但我不明白我做错了什么。事实上,它正确地删除了图像路径,而不是“|”字符。因此,例如在第一个循环之后,测试字符串变为:
|/mnt/sdcard/Download/images.jpeg|/mnt/sdcard/Download/images-1.jpeg|/mnt/sdcard/Download/images-2.jpeg|
这是不正确的,因为开头有“|”字符。这是我的代码。
for(int i=0; i<=3; i++) {
//find the characters number before you find the first " | " character available
int indexOfStatic = test.indexOf("|");
//this find the string that we want remove from the test string that contains all paths
int indexOfStaticToRemove = test.indexOf("|")+1;
String testPath = test.substring(0, indexOfStatic); //this is the correct path of the image
String testPathToRemove = test.substring(0, indexOfStaticToRemove); //this is the path with the " | " character that we want remove from the test string
Log.i("PATH TO REMOVE",""+testPathToRemove);
//here i remove the path with the " | " character. I use the replaceFirst method because if the "test" string contains two equals paths (how in my example) i want to replace only one at time for avoid crash during the loop
test = test.replaceFirst(testPathToRemove,"");
Log.i("TEST REPLACE",""+test); //Replace the first | with nothing
}
我不明白哪个是问题,但实际上应该不难。