Can the switch statement be used with strings?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Can the switch statement be used with strings??

15th May 2019, 8:35 AM
//(AnOnYmOuS);
8 Answers
+ 5
Mohd Shahzer String in Switch Case in Java. Beginning with JDK 7, we can use astring literal/constant to control a switch statement, which is not possible in C/C++. Using a string-based switch is an improvement over using the equivalent sequence of if/else statements. source: https://www.geeksforgeeks.org/string-in-switch-case-in-java/ I hope I was helpful
15th May 2019, 9:02 AM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 8
@Alessio Yes you were Thanks
15th May 2019, 9:07 AM
//(AnOnYmOuS);
+ 4
As of now, there is no known way of using strings with switch case. You could use single character constants instead, or maybe some character at some position in the string.
15th May 2019, 9:45 PM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 2
Mohd Shahzer you are welcome!
15th May 2019, 9:16 AM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 2
Not possible till now with C++ . But you can use a single character instead of string in switch statement.
19th May 2019, 6:38 AM
Rhythm Sehgal
Rhythm Sehgal - avatar
+ 1
We can't use a switch statement with strings in c++. you can only use integer, short, long, character. I think its the same for all the other programming languages too. correct me if i am wrong.
16th May 2019, 7:07 PM
Gothama Rajawasam
Gothama Rajawasam - avatar
+ 1
Bennett Post 😊 thanks 😊
16th May 2019, 7:16 PM
Gothama Rajawasam
Gothama Rajawasam - avatar
+ 1
c#,java string base switch allowed.
15th Jun 2019, 4:09 PM
Vaibhav Raut
Vaibhav Raut - avatar