简体   繁体   English

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

[英]Having difficulties to remove a substring from a string during a for loop

I'm trying to work with strings. 我正在尝试使用字符串。 In my case i have this 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|";

This string contains 4 paths of images in my android emulator. 这个字符串包含我的Android模拟器中的4个图像路径。 Every path is separated by the " | " character as you can see. 如您所见,每个路径都由“ |”字符分隔。 What i want to do is enough simple. 我想做的很简单。 I have to create a for loop where i find every single path without the " | " character and on each loop i need to remove the path founded, but in this case with the " | " character. 我必须创建一个for循环,在该循环中我找到每个没有“ |”字符的路径,并且在每个循环上我都需要删除已建立的路径,但在这种情况下,需要使用“ |”字符。 I have implemented the code but i'm not understanding what i'm doing wrong. 我已经实现了代码,但是我不明白自己在做什么错。 In fact it remove correctly the image path but not the " | " character. 实际上,它可以正确删除图像路径,但不能删除“ |”字符。 So for example after the first loop the test string become: 因此,例如,在第一个循环之后,测试字符串变为:

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

that isn't correct because there is the " | " character at the start. 这是不正确的,因为开头有“ |”字符。 Here there is my code. 这是我的代码。

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
}

I'm not understanding which is the problem, but in fact it shouldn't be difficult. 我不知道这是问题所在,但实际上应该不难。

You are overlooking in to the problem, use split() 您正在忽略问题,请使用split()

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

}

Which gives you the required paths: 这为您提供了所需的路径:

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

Looks like no one is answering the real question. 似乎没人在回答真正的问题。

Look at the documentation on replaceFirst() method: 查看关于replaceFirst()方法的文档:

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

It uses regex to replace the part. 它使用正则表达式替换零件。 But | | in regex is OR statement. 在正则表达式中是OR语句。 That is causing the confusion you have 那会引起你的困惑

You should use the split method: 您应该使用split方法:

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

The source of your problem is because of the line: 问题的根源在于:

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

Here replaceFirst will take in a regular expression in the first argument, in your case it will be '/mnt/sdcard/Download/images.jpeg|' 在这里,replaceFirst将在第一个参数中接受一个正则表达式,在您的情况下,它将为“ /mnt/sdcard/Download/images.jpeg |” Note there is a '|' 注意有一个“ |” and that '|' 那'|' will be inferred as an OR operation and hence it can take anything as your string to be replaced. 将被推断为“或”运算,因此可以将任何内容作为要替换的字符串。 That is why after you execute that line, test will become and empty string. 这就是为什么在执行该行之后,测试将变为空字符串。

You can try using substring again instead to get the remaining string. 您可以尝试再次使用子字符串来获取剩余的字符串。

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();
    }
 }

Yes best option would be use 是的,最好的选择是使用

test.split("\\|") 

and you will get required array or you can use other symbol instead of | 您将获得所需的数组,或者可以使用其他符号代替| ( because it used as OR in regex)in replaceFirst like "#" or "$" (因为它在正则表达式中用作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

} }

it gives following output 它给出以下输出

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
test = test.replaceFirst(testPathToRemove,"");

The first parameter in method 'replaceFirst' is not a String, but is a regular expression. 方法“ replaceFirst”中的第一个参数不是字符串,而是正则表达式。 As other guys said you can use below code instead of your code. 正如其他人所说,您可以使用以下代码代替您的代码。 It's very simple. 非常简单

test.split("\\|");

If you still want to use your code, you can use below code: 如果仍要使用代码,则可以使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM