Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Case Statement with String

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; 
like image 444
Glen Morse Avatar asked Jan 25 '13 04:01

Glen Morse


People also ask

Can we use string in case statement?

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

Can I use string in Switch Case C++?

You cannot use string in either switch or case .

Can case be a string in Java?

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.

Can we use and in case statement in SQL?

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 .


2 Answers

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. 
like image 144
Daniel Avatar answered Sep 23 '22 01:09

Daniel


The Delphi Case Statement only supports ordinal types. So you cannot use strings directly.

But exist another options like

  • build a function which returns a Integer (hash) based on a string
  • using generics and anonymous methods ( A generic case for strings)
  • using a function which receive an array of strings (Making a case for Strings, the sane way)
  • and so on.
like image 36
RRUZ Avatar answered Sep 22 '22 01:09

RRUZ