How to create switch for strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to create switch for strings?

13th Nov 2016, 2:31 AM
-Diamond Armor -
-Diamond Armor - - avatar
6 Answers
+ 2
Good question, it stumped me. But, from my short search, there is no such thing. At least, not an easy way to implent it.
13th Nov 2016, 2:55 AM
Keto Z
Keto Z - avatar
+ 2
Switches don't work with strings or floating point numbers. You are limited to integers, characters, booleans and enumerations.
13th Nov 2016, 4:01 AM
Cohen Creber
Cohen Creber - avatar
+ 1
Well you aren't able to do it I'm afraid. For strings, use else-if, if (str == "Hi") { } else if (str == "Hello") { } //if necessary, add more else-if else { }
13th Nov 2016, 4:23 AM
Cohen Creber
Cohen Creber - avatar
0
I guess its only compatible with intergers?
13th Nov 2016, 3:53 AM
Ziyaan Hassan
Ziyaan Hassan - avatar
0
I need it because I want to create some like command line( in OS Windows). I can use "if", but "switch" is more compact.
13th Nov 2016, 4:17 AM
-Diamond Armor -
-Diamond Armor - - avatar
0
i never really thought of this, very interesting
14th Nov 2016, 7:53 AM
Sabata Mofokeng
Sabata Mofokeng - avatar