0

I have to check the return value of the method, if it equals the value of a string which is either "Yes" or "No" or should I use a regex which matches with the pattern of the string i.e Yes or No.

So essentialy, I am trying to write in this way if ((ru.getTest()).equals(("Yes") || (“No”))) {......

Is it the way , I should proceed or am wrong?

Adding to it.

I would like to post a detailed explanation of my question.

if(ru.getTest().equals("yes/oui") || ru.getTest().equals("no/non")),

It is not just Yes but along with the French literal added to it. So, if (the above condition is met), it should trim the French literal portion and keep only the 'yes' or 'no'..

I tried with this:
if ((ru.getTest()).equals("Yes/Oui") || (ru.getTest()).equals(“No/Non”)) { Test = ru.getTest().substring(0, Test.indexOf ('/'));

Please correct me If I am wrong.

Soom Satyam
  • 25
  • 1
  • 1
  • 7

3 Answers3

0

You can basically just write:

if(ru.getTest().equals("yes") || ru.getTest().equals("no"))

I believe a simple check can be done by using equals and not necessarily using regex. Regex tends to be used for more complex matching scenarios.

  • Thanks @Luci. I would like to post a detailed explanation of my question.' if(ru.getTest().equals("yes/oui") || ru.getTest().equals("no/non")), It is not just Yes but along with the French literal added to it. So, if (the above condition is met), it should trim the French literal portion and keep only the 'yes' or 'no'.. I tried with this: if ((ru.getTest()).equals("Yes/Oui") || (ru.getTest()).equals(“No/Non”)) { Test = ru.getTest().substring(0, Test.indexOf ('/')); Is it a correct approach?? – Soom Satyam May 24 '16 at 18:07
0

You can write like this.

String s = ru.getTest().toLowerCase();
if(s.contains("yes") || s.contsins("oui")) {
    Test = "yes";
} else {
    Test = "no";
}

If there are different input, use s.matchs() method for regular expression

0

Use matches() for regex:

if (ru.getTest().matches("(?i)yes|no"))

If you just want to trim the part after and including a possible slash, you don't even need to check anything first:

String trimmed = ru.getTest().replaceAll("/.*", "");

This will work whether or not there is a slash.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Could you check if my approach is correct with the detailed explanation of my question, I asked again. – Soom Satyam May 24 '16 at 20:49
  • @soom see if the edit to my answer works for you. I'm still not understanding what your problem is exactly. – Bohemian May 24 '16 at 22:25
  • I simply want to check: 1) if the getTest method returns a string of value "Yes/Oui' or "No/Non" 2)If it matches, then trim the portion of string value i.e omitting / and after that which means simply Yes or No,, it should return and that return value, I need to store in a local variable declared. – Soom Satyam May 25 '16 at 00:03