Beginners question about switching on String object contents

Hi All....
Im fairly new to this, so sorry if this question is a little basic....
Was wondering how I can go about switching on the contents of a String object.

  String MsgDest;
  MsgDest = "sdt";

  switch('MsgDest') 
  {
         Serial.print(MsgDest);
            Serial.print(MsgDest.length());

    case 'sdt'://sequ delay time
       Serial.print("inside case \r\n");
       Serial.print(MsgDest);
       MsgDest = "";
       Serial.print(MsgDest);
       Serial.print(MsgDest.length());
   break;

While this compiles, the "sdt" case is never hit.
I know its wrong, bur how can I go about doing this please??

Incidentally, can I empty the string object simply by assigning it an empty string
MsgDest = ""; like that ???

Thanks in advance for your time !

Phil

Was wondering how I can go about switching on the contents of a String object.

You don't, basically. Switch is for scalar values (eg. numbers). Also there is a big difference between 'MsgDest' and "MsgDest". The one with double-quotes is a pointer to a character array.

This is strange:

MsgDest = "sdt";

  switch('MsgDest')

You are not switching on the contents of MsgDest you know. You are switching on some strange literal the compiler has generated, out the ANSI values of M s g D e s t. I hate to think what that value is.

Incidentally, can I empty the string object simply by assigning it an empty string

Yes you can do that, however I would be weaning yourself off the String class. It tends to chew up memory.

Here's an informative little test program:

int m = 'MsgDest';

void setup()
{  Serial.begin(115200);
   Serial.println(m,HEX);
}
void loop()
{
}

It appears that SurfCracker is switching on the final 16 bits ('st'). It makes sense - in a very twisted kind of way. C (and therefore C++) do have a few ugly warts... :grin:

Switch is for numeric data. You want to use if statements to compare strings.

And you really want to dump the C++ String Class for C character arrays in the tiny MCU environment. They are simpler and better behaved provided you don't write past the end of the array, ie color within the lines you drew. Sure, you can get away with it but remember and later on when you have String code that mysteriously crashes you'll have one more place to look.

And another:

void setup() {
  Serial.begin(115200);

  union {
     long long a;
     char b [sizeof (long long)];
  } foo;
  
  foo.a = 'abcdefgh';
  
  for (int i = 0; i < sizeof foo.b; i++)
    Serial.println (foo.b [i], HEX);
}

void loop() { }

Output:

68
67
0
0
0
0
0
0

The interesting things we get out this are, first that the byte order is little endian (so we (would) actually see 'hgfedcba' in that order).
Second, the compiler is truncating the generated numeric literal to "int" size (2 bytes), so we only actually get the 'h' and the 'g'.

My attempts to do this met with a compiler error:

  foo.a = 'abcdefgh'LL;

This compiles but makes no difference:

  foo.a = (long long) 'abcdefgh';

@op: you can see that 'MsgDest' is going to be a completely different beast to "MsgDest". The first one will actually generate: 0x74 0x73 0x00 0x00 0x00 0x00 0x00. The second will generate a pointer to where the string "MsgDest" is stored in memory. Neither of these will compare equal to "sdt".