Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use .contains() in a switch statement?

This is just a simple example of what I'm trying to do:

switch (window.location.href.contains('')) {     case "google":         searchWithGoogle();         break;     case "yahoo":         searchWithYahoo();         break;     default:         console.log("no search engine found"); } 

If it's not possible/feasible what would be a better alternative?

Solution:

After reading some of the responses I found the following to be a simple solution.

function winLocation(term) {     return window.location.href.contains(term); } switch (true) {     case winLocation("google"):         searchWithGoogle();         break;     case winLocation("yahoo"):         searchWithYahoo();         break;     default:         console.log("no search engine found"); } 
like image 879
RayB Avatar asked Jul 19 '14 17:07

RayB


People also ask

What is not allowed in a switch statement?

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed. The case statements and the default statement can occur in any order in the switch statement.

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.

Can switch-case contains String?

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

Can switch-case contain expression?

Expression SwitchYou can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.


2 Answers

"Yes", but it won't do what you expect.

The expression used for the switch is evaluated once - in this case contains evaluates to true/false as the result (e.g. switch(true) or switch(false)) , not a string that can be matched in a case.

As such, the above approach won't work. Unless this pattern is much larger/extensible, just use simple if/else-if statements.

var loc = .. if (loc.contains("google")) {   .. } else if (loc.contains("yahoo")) {   .. } else {   .. } 

However, consider if there was a classify function that returned "google" or "yahoo", etc, perhaps using conditionals as above. Then it could be used as so, but is likely overkill in this case.

switch (classify(loc)) {    case "google": ..    case "yahoo": ..    .. } 

While the above discusses such in JavaScript, Ruby and Scala (and likely others) provide mechanisms to handle some more "advanced switch" usage.

like image 136
user2864740 Avatar answered Sep 21 '22 10:09

user2864740


An alternative implementation might be this. Not much in it but reads better than switch(true)...

const href = window.location.href; const findTerm = (term) => {   if (href.includes(term)){     return href;   } };  switch (href) {   case findTerm('google'):       searchWithGoogle();       break;   case findTerm('yahoo'):       searchWithYahoo();       break;   default:       console.log('No search engine found'); }; 
like image 35
TimT Avatar answered Sep 23 '22 10:09

TimT