-4

Say if I have the following code

String sum = "(5+5)/2*6";
char[] bodmasChars = {'+','-','*','/','.'.'(',')'};

Is there a way to check whether the string contains any of the elements in my char[]?

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
BigAl1992
  • 67
  • 1
  • 2
  • 11
  • 1
    Did you take a look at the methods that the ``String`` class offers you? – f1sh Dec 05 '17 at 10:04
  • Yes, there is, and any google search for "[java string contains char](https://www.google.com/search?q=java+string+contains+char)" will show you how. Once you have an actual problem, please ask, but do your research first. – Malte Hartwig Dec 05 '17 at 10:08
  • 1
    Possible duplicate of [How can I check if a single character appears in a string?](https://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string) – Malte Hartwig Dec 05 '17 at 10:08

2 Answers2

0

A regex

String sum = "(5+5)/2*6";
if (sum.matches("(?s).*[-\\+\\*/\\.()].*")) { ...

(?s) lets . also match newlines. [...] is group of possible character or character ranges. Probably have a backslash too many.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You can try it with Regex or with Simple as :

String s="(5+5)/2*6";
        if((s.contains("+")||(s.contains("-")||(s.contains("-"))||....))
                {
            System.out.println("yes");
                }
Lalit Verma
  • 782
  • 10
  • 25