简体   繁体   English

将字符串与 ArrayList 字符串元素进行比较

[英]Compare a string with an ArrayList string element

I need some help to fix my issue as I described below:我需要一些帮助来解决我的问题,如下所述:

I have an array list with name datalist and it has elements like ah3w 38 45 1, zj4e 32 4 45. I want to compare a string like ah3w 38 45 1 for checking is it in arraylist or not.我有一个名为 datalist 的数组列表,它有像 ah3w 38 45 1, zj4e 32 4 45 这样的元素。我想比较像 ah3w 38 45 1 这样的字符串来检查它是否在 arraylist 中。 The main problem is whitespaces.主要问题是空格。 I want to ignore them because sometimes I have elements in arraylist which has more than one white space between characters.我想忽略它们,因为有时我在 arraylist 中的元素在字符之间有多个空格。 I had tried something like below with using replaceAll method, but it didnt work.我曾尝试使用 replaceAll 方法进行类似下面的操作,但没有奏效。 Lastly I tried to remove all white space before I created the arraylist, then I did same thing for my string too, it worked but it is not like I want.最后,我尝试在创建数组列表之前删除所有空格,然后我也为我的字符串做了同样的事情,它有效,但它不是我想要的。 I dont want to change anything I just want to compare them without spaces.我不想改变任何东西,我只想在没有空格的情况下比较它们。 Here is my code example:这是我的代码示例:

String field = "ah3w 38 45 1"
for(int a=0; a<datalist.size(); a++){
   if(datalist.get(a).replaceAll("\\s+","").contains(field.replaceAll("\\s+",""))){
      datalist.remove(a);
   }
}

Here is another code block that is doing same thing and you can run and see what I am trying to do and what ı am getting:这是另一个执行相同操作的代码块,您可以运行并查看我正在尝试做什么以及我得到了什么:

import java.util.ArrayList;

public class ReplaceAllElementsOfArrayListExample {

  public static void main(String[] args) {

    ArrayList arrayList = new ArrayList();
    String field= "ah3w 38 45 1";

    arrayList.add("zj4e 32   4  45");
    arrayList.add("ah3w       38  45   1");
    arrayList.add("ab2 56 2 45");

    System.out.println("Before replacement, ArrayList contains : " + arrayList);

    if(arrayList.get(1).replaceAll("\\s+","").contains(field.replaceAll("\\s+",""))){

    System.out.println("They are equal : " + arrayList);}
    else{
      System.out.println("They are not equal : " + arrayList);  
    }

  }
}

Try this:尝试这个:

    ArrayList<String> arrayList = new ArrayList<String>();
        String field = "ah3w 38 45 1";

        arrayList.add("zj4e 32   4  45");
        arrayList.add("ah3w       38  45   1");
        arrayList.add("ab2 56 2 45");

        //solution
        String fieldNoWhitespaces = field.replaceAll("\\s", "");

        for (String dataElement : arrayList) {
            String arg = dataElement.replaceAll("\\s", "");
            if (fieldNoWhitespaces.equals(arg)) arrayList.remove(dataElement);      
        }
        
        //just to check for correctness
        for (String dataElement : arrayList) {
            System.out.println(":::" + dataElement);

            int count = 0;
            for (int i = 0; i < dataElement.length(); i++) {
                if (Character.isWhitespace(dataElement.charAt(i))) {
                    count++;
                }
            }

        System.out.println(":::Whitespaces:::" + count);
    }

Output:输出:

:::zj4e 32 4 45 :::zj4e 32 4 45

:::Whitespaces:::6 :::空格::::6

:::ab2 56 2 45 :::ab2 56 2 45

:::Whitespaces:::3 :::空格::::3

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

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