Say i have a string
'SomeName'
and wanted the values return in a case statement. Can this bedone? Can strings be used in a case statement like so
Case 'SomeName' of 'bobby' : 2; 'tommy' :19; 'somename' :4000; else showmessage('Error'); end;
Yes, we can use a switch statement with Strings in Java.
You cannot use string in either switch or case .
Java switch case String is case sensitive, the output of example confirms it. Java Switch case uses String. equals() method to compare the passed value with case values, so make sure to add a NULL check to avoid NullPointerException.
CASE must include the following components: WHEN , THEN , and END . ELSE is an optional component. You can make any conditional statement using any conditional operator (like WHERE ) between WHEN and THEN . This includes stringing together multiple conditional statements using AND and OR .
In Jcl library you have the StrIndex function StrIndex(Index, Array Of String)
which works like this:
Case StrIndex('SomeName', ['bobby', 'tommy', 'somename']) of 0: ..code.. ;//bobby 1: ..code..;//tommy 2: ..code..;//somename else ShowMessage('error'); end.
The Delphi Case Statement
only supports ordinal types. So you cannot use strings directly.
But exist another options like
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With