简体   繁体   English

在Java中一次搜索两个字符串

[英]Search for two strings at once in java

So on a basic level is there a way to say (maybe using an operator) look for this string OR this one within the same variable declaration. 因此,从根本上讲,有一种方法可以说(也许使用运算符)在相同的变量声明中查找此字符串或该字符串。 (maybe an array). (也许是数组)。 I've tried operators such as the pipe and & and it doesn't work with a single string variable input. 我尝试了管道和&之类的运算符,但它不适用于单个字符串变量输入。 I have also tried putting the strings into an array and using the string value of that array but all to no avail. 我也尝试过将字符串放入数组并使用该数组的字符串值,但无济于事。 Thank you 谢谢

Elements names= doc.body().getElementsContainingOwnText("Mr.");
for (Element web2: names) {
    Log.i("names", web2.text());
}

I also want to check for "Ms." 我还想检查“女士”。 but do it at the same time because sequence matters in this instance. 但是要同时执行,因为顺序在这种情况下很重要。 By the way this is the jsoup SDK 1.8.3. 顺便说一下,这是jsoup SDK 1.8.3。

You can use the method getElementsMatchingOwnText(String regex) . 您可以使用方法getElementsMatchingOwnText(String regex)

So to match Mr. or Ms. your query will be something like, 因此,要匹配Mr.Ms.您的查询将类似于:

Elements names= doc.body().getElementsMatchingOwnText("Mr\\.|Ms\\."); // Escape . in regex.

You can try Java8 Stream 您可以尝试Java8 Stream

    String s1 = "111";
    String s2 = "222";
    boolean res = Arrays.asList(s1,s2).stream().anyMatch(s->"333".equals(s));

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

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