在for循环期间难以从字符串中删除子字符串

时间:2013-10-08 08:05:45

标签: java string

我正在尝试使用字符串。在我的情况下,我有这个字符串:

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
}

我不明白哪个是问题,但事实上它应该不难。

6 个答案:

答案 0 :(得分:4)

您正在忽略问题,请使用split()

    String[] imagePaths = test.split("\\|");
    for (String string : imagePaths) {
        System.out.println(string);
    }

}

它为您提供了所需的路径:

    /mnt/sdcard/Download/images.jpeg
    /mnt/sdcard/Download/images.jpeg
    /mnt/sdcard/Download/images-1.jpeg
    /mnt/sdcard/Download/images-2.jpeg

答案 1 :(得分:2)

看起来没有人回答真正的问题。

查看replaceFirst()方法的文档:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

它使用正则表达式来替换零件。但正则表达式中的|OR语句。这导致了你的混乱

答案 2 :(得分:0)

您应该使用拆分方法:

String[] paths = test.split("\\|");
for (int i=0; i<paths.length; i++) {
  //in paths[i] you have the i-esim path
}

答案 3 :(得分:0)

问题的根源是因为行:

test = test.replaceFirst(testPathToRemove,"");

这里replaceFirst会在第一个参数中包含一个正则表达式,在你的情况下它将是'/mnt/sdcard/Download/images.jpeg |'注意有一个'|'那个'|'将被推断为OR操作,因此它可以取代任何东西作为您的字符串被替换。这就是为什么在执行该行后,test将变为空字符串。

您可以尝试再次使用子字符串来获取剩余的字符串。

public static void main(String []args){
    String test = "/mnt/sdcard/Download/images.jpeg|/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
            System.out.println("PATH: "+testPath);
            System.out.println("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.substring(indexOfStaticToRemove,test.length());
            System.out.println("PATH TO REPLACE: "+test);
            System.out.println();
    }
 }

答案 4 :(得分:0)

是的最佳选择是使用

test.split("\\|") 

您将获得所需的数组,或者您可以使用其他符号而不是| (因为它在正则表达式中用作OR)在replaceFirst中用作“#”或“$”

    String test ="/mnt/sdcard/Download/images.jpeg#/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, indexOfStaticToRemove); //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
            System.out.println("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,"");
            System.out.println("TEST REPLACE"+test); //Replace the first | with nothing

}

它提供以下输出

PATH TO REMOVE/mnt/sdcard/Download/images.jpeg#
TEST REPLACE/mnt/sdcard/Download/images.jpeg#/mnt/sdcard/Download/images-1.jpeg#/mnt/sdcard/Download/images-2.jpeg#
PATH TO REMOVE/mnt/sdcard/Download/images.jpeg#
TEST REPLACE/mnt/sdcard/Download/images-1.jpeg#/mnt/sdcard/Download/images-2.jpeg#
PATH TO REMOVE/mnt/sdcard/Download/images-1.jpeg#
TEST REPLACE/mnt/sdcard/Download/images-2.jpeg#
PATH TO REMOVE/mnt/sdcard/Download/images-2.jpeg#
TEST REPLACE

答案 5 :(得分:0)

test = test.replaceFirst(testPathToRemove,"");

方法'replaceFirst'中的第一个参数不是String,而是正则表达式。正如其他人说你可以使用下面的代码代替你的代码。这很简单。

test.split("\\|");

如果您仍想使用代码,可以使用以下代码:

test = test.replaceFirst(testPathToRemove.replaceAll("\\|", "\\\\\\|"), "");