how to check if there is a symbol on a String java

Viewed 42

I want to know if is there a method to check if there are any symbols in a string, I know that to check if there's an uppercase is used:

enter code herecharacter.isUpperCase(letter);

is there any option such as this but for special symbols like: !@#$%%^"

1 Answers

Take "!@#$%%^" as a string say A and then run 1 loop i from 0 to less than length of the input string stored in say S and another loop j from 0 to length of A ( j loop is nested within i loop) now check if ( S.charAt(i) == A.charAt(j) ) { // relevant code}

as in:

String S= br.readLine();
String A= "!@#$%%^";
for ( int i=0; i< S.length(); i++)
 {for(int j=0;j< A.length(); j++)
    {
      if (S.charAt(i) == A.charAt(j)) 
       {// relevant code
        }
     }
}

this brings about the worst case complexity to be O(n²) , but considering the point of view of a school student I view this as the easiest one for academic purposes

Related