1

我正在尝试使用字符串。就我而言,我有这个字符串:

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
}

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

4

6 回答 6

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
于 2013-10-08T08:11:29.423 回答
2

似乎没有人回答真正的问题。

查看有关replaceFirst()方法的文档:

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

它使用正则表达式来替换零件。但|在正则表达式中是OR声明。这导致了你的困惑

于 2013-10-08T08:11:53.057 回答
0

您应该使用拆分方法:

String[] paths = test.split("\\|");
for (int i=0; i<paths.length; i++) {
  //in paths[i] you have the i-esim path
}
于 2013-10-08T08:08:30.723 回答
0

你的问题的根源是因为这条线:

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

这里 replaceFirst 将在第一个参数中采用正则表达式,在您的情况下它将是 '/mnt/sdcard/Download/images.jpeg|' 注意有一个'|' 还有那个'|' 将被推断为 OR 操作,因此它可以将任何内容作为要替换的字符串。这就是为什么在您执行该行之后,测试将变为空字符串。

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

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();
    }
 }
于 2013-10-08T08:21:45.707 回答
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
于 2013-10-08T08:34:36.083 回答
0
test = test.replaceFirst(testPathToRemove,"");

方法“replaceFirst”中的第一个参数不是字符串,而是正则表达式。正如其他人所说,您可以使用下面的代码而不是您的代码。这很简单。

test.split("\\|");

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

test = test.replaceFirst(testPathToRemove.replaceAll("\\|", "\\\\\\|"), "");
于 2013-10-08T09:04:40.003 回答