【问题标题】:check if string array contains value检查字符串数组是否包含值
【发布时间】:2018-08-18 17:32:56
【问题描述】:

我必须检查字符串数组test2[] 是否包含test1[] 的值。怎么做?两个数组的大小不同。我还必须检查test2[] 是否包含test1[] 的子字符串。

String[] test = {"Test1", "Test2"};
String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};

非常感谢。

【问题讨论】:

  • 2 个 foreach 循环可以解决问题
  • “包含”在这里太笼统了。请澄清 - test2 应该包含 test 作为子集,或者它应该包含来自 test 的任何值,或者它应该包含 test 的(连续或不连续的)子序列
  • HashSet + contains 可以解决问题。

标签: java arrays string substring


【解决方案1】:

您只需要几个嵌套循环并进行迭代。您的问题模棱两可,您所说的 contains 可能意味着 equalscontains the substring。无论哪种情况,如果您想要相等匹配,只需将.contains() 替换为.equals()

for (String value : test) {
        for (String sampleString : test2) {
            if (sampleString.contains(value)) {
                System.out.println("Value " + value + " is contained in the array in " + sampleString);
            }
        }
    }

【讨论】:

    【解决方案2】:

    您可以使用两个 for 循环,一个用于迭代 test,另一个用于检查项目是否包含在 test2

    public static boolean checkIfExists(String[] arr, String item) {
        for (String n : arr) {
    
             if (n.contains(item)) {
                return true;
             }
          }
          return false;
       }
    

    在main方法中

    String[] test = {"Test1", "Test2"};
    String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};
    for(String t : test) {
        System.out.println(checkIfExists(test2, t));
    }
    

    【讨论】:

      【解决方案3】:
      public boolean checkIfHaveSameElements(String[] test1, String[] test2) {
          for (String str2 : test2) {
              for (String str1 : test1) {
                  if (str2.equals(str1)) {
                      return true;
                  }
              }
          }
          return false;
      }
      

      只需将您的数组作为方法调用参数传递。

      【讨论】:

        【解决方案4】:

        使用 Java 8 API,我首先检查整个字符串是否存在或子字符串是否存在。

        public class CheckString {
        
        public static void main(String[] args) {      
        
            String[] test = {"Test1", "Test3", "Test2"};
            String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};
        
            boolean isPresent = Arrays.stream(test2)
                                      .filter(str->{
                                       return Arrays.asList(test).indexOf(str) > 0 || checkString(test,str);
                                       })
                                      .collect(Collectors.toList())
                                      .isEmpty();
        
            System.out.println(!isPresent);
        
            }
            private static boolean checkString(String[] strs, String chkStr){
                for(String str: strs){
                    if(chkStr.contains(str)){
                        return true;
                    }
                }
                return false;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-05-06
          • 1970-01-01
          • 2012-08-11
          • 1970-01-01
          • 2022-06-30
          • 2021-12-14
          • 2016-06-07
          • 2013-11-12
          • 2015-02-28
          相关资源
          最近更新 更多