String包含或不包含Java中的子字符串

时间:2017-04-29 11:45:47

标签: java string arraylist netbeans compare

实际上我在比较java中的字符串时遇到了问题。问题是我有Arraylist类型列表,其中包含一些员工名称,但指定可能是开发人员或初级开发人员或可能是Sinior开发人员类型指定我的问题,考虑所有开发人员指定作为唯一的开发人员,所以我使用此代码。

ArrayList<String> al_designation = ret.getDesignationListFromRegistration();
int ii = 0;
for (String designation : al_designation) {
    ii++;
    for (int j = ii; j < al_designation.size(); j++) {
        if (designation.toLowerCase().contains(al_designation.get(j).toLowerCase())) {
            al_designation.remove(j);
        }
    }
}

enter image description here

enter image description here

我想要的所有HELPER类型指定仅考虑HELPER,对于STITCHER所有类型的STITCHER都相同。

2 个答案:

答案 0 :(得分:3)

将ArrayList中的必需项添加到HashMap中,如下所示:

    ArrayList<String> al_designation = new ArrayList<>(Arrays.asList("Developer", "Junior Developer",
            "Senior Developer", "Senior Architect", "Junior Manager", "Senior Manager", "Assistant Manager", //
            "CEO", "Tech Lead", "Dev Lead"));
    System.out.println(al_designation);// Before it contains all
                                        // designations.

    Map<String, String> map = new LinkedHashMap<>();
    for (String designation : al_designation) {
        designation = designation.toLowerCase();
        String[] words = designation.split(" ");
        String lastWord = words[words.length - 1];
        String value = map.get(lastWord);
        if (value == null)
            map.put(lastWord, designation);
        else
            map.put(lastWord, lastWord);
    }
    al_designation.clear();
    al_designation.addAll(map.values());
    System.out.println(al_designation);// Now it contains only required designations. 
                                        //This is your answer.

答案 1 :(得分:2)

我不知道你想要完成什么,但是你可以查看你的字符串是否是“初级开发人员”包含子串“开发者”,如下所示:

if (str1.toLowerCase().contains(str2.toLowerCase())){
    // do whatever you need to do
}