Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java string.contains in switch statement

Tags:

People also ask

Can we use contains in switch case Java?

You can't switch on conditions like x. contains() . Java 7 supports switch on Strings but not like you want it. Use if etc.

Can you use Strings in switch statement Java?

Yes, we can use a switch statement with Strings in Java.

Can you use Strings in switch?

String is the only non-integer type which can be used in switch statement.

Does switch support String data type?

A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer (discussed in Numbers and Strings).


How can I convert the following code to switch statement?

String x = "user input";

if (x.contains("A")) {
    //condition A;
} else if (x.contains("B")) {
    //condition B;
} else if(x.contains("C")) {
    //condition C;
} else {
    //condition D;
}