简体   繁体   English

Java,如果部分匹配

[英]Java if else on partial match

I've moved into Selenium WebDriver, and still finding the most confusing examples. 我已经进入Selenium WebDriver,并且仍然找到最令人困惑的示例。

I need to be able to read a string (succeeded) run a conditional that asks If specific text is present. 我需要能够读取一个字符串(成功),并运行一个条件查询,询问是否存在特定的文本。

For the sake of this text. 为了本文的缘故。

String oneoff = "Jeff is old"

I need to match on Jeff, see code below, as long as Jeff exists in the string, I want to return true. 我需要匹配Jeff,请参见下面的代码,只要Jeff存在于字符串中,我就想返回true。 If Jeff doesn't exist, then I will check for oh say 50-75 other names. 如果杰夫不存在,那么我会检查哦,说50-75个其他名字。 However the string may contain their name and additional text that cannot be controlled. 但是,字符串可能包含其名称和其他无法控制的文本。 so I have to do a partial match. 所以我必须做部分比赛。

Question 1. am I screwed and will have to build each regex expression in that crazy format that I have been seeing, or am I missing something obvious? 问题1.我是否搞砸了,是否必须以我见过的那种疯狂格式构建每个正则表达式,还是我遗漏了一些明显的东西?

Question 2. Will someone for my sanity please show me the proper way to match on Jeff, with the possibility of text being before and after the name Jeff. 问题2。请问有人出于我的理智,请告诉我匹配Jeff的正确方法,文字可能在Jeff的名字之前和之后。

Thank you! 谢谢!

String oneoff = driver.findElement(By.id("id_one_off_byline"))
        .getAttribute("value");
System.out.println("One Off is:" + oneoff);
if (oneoff.matches("Jeff")) {
    System.out.println("It is Jeff");
} else {
    System.out.println("it is not jeff");
}

This is just the functional part of the code, 这只是代码的功能部分,

as Jeff exists in the string, I want to return true 因为Jeff存在于字符串中,所以我想返回true

Then you probably should test it with 那你可能应该用

if (oneoff.contains("Jeff")) 

since matches use regex as parameter, so if (oneoff.matches("Jeff")) would return true only if oneoff = "Jeff" . 由于matches使用正则表达式作为参数,因此if (oneoff.matches("Jeff"))仅在oneoff = "Jeff"时才返回true。

You do not need to use match() for the code you have supplied. 您无需为提供的代码使用match()。 Instead use oneoff.equals("String") for string matching. 而是使用oneoff.equals(“ String”)进行字符串匹配。 Match() is more for a regex expressions. Match()更适合于正则表达式。 You could also use oneoff.contains("String") if you want to return true even if the string only exists as a subset of the target string. 如果您想返回true,即使该字符串仅作为目标字符串的子集存在,也可以使用oneoff.contains(“ String”)。

if (oneoff.contains("Jeff")) {
    System.out.println("It is Jeff");
} else if (!oneoff.contains("Jeff")) {
    System.out.println("it is not jeff");
}

I think you should improve your code to be like this, because java probably didn't recognize else string if contained with other "jeff" maybe "JEef" or "JEEF" or even maybe "Jeef " 我认为您应该改进代码,因为如果其他“ jeff”(例如“ JEef”或“ JEEF”或什至“ Jeef”)中包含Java,则Java可能无法识别其他字符串

I hope it works, I used to found same bug like yours and I try this way to overcome it. 我希望它能起作用,我曾经发现过像您一样的错误,然后尝试通过这种方式来克服它。

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

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