java 基础问题,一个字符数组中,是否存在一个元素,在一个长字符串中?
大家讲道理
大家讲道理 2017-04-18 09:51:56
0
3
620

代码如下:

String urlString="http://localhost:8080/adm/subComInfo/propConfigList";
if (urlString.contains("list") || urlString.contains("/propConfigList") ) {
                manageLog.setOperateType("查询");

要判断urlString,是否包含,list 等多个子字符串,用上面这种写法是可以实现,

问题,如果要判断的子字符串有很多个,就不太美观,请问,是否有一个集合工具方法,能够让我,判断一个长字符串中,如果,包含多个子字符串中的一个,如果包含就返回true?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
小葫芦

Why not try regular expressions?

小葫芦

function boolean s1(String longString, List<String> StrList){

for(String str : StrList){
    if(longString.contains(str)){
        return true;
    }
}
return false;

}

Peter_Zhu

Is your requirement to determine whether an element in a string array is a substring of another string? If so, you can write a method:

public class Main {

    public static void main(String[] args) {
        String[] strings = {"www", "http"};
        String sourceString = "https://www.baidu.com/";
        boolean contain = contains(sourceString, strings);
        System.out.println(contain);//是否包含
    }

    private static boolean contains(String sourceString, String[] strings) {
        boolean contain = false;
        for (String s : strings) {
            if (sourceString.contains(s)) {
                contain = true;
            }
        }
        return contain;
    }

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!