Serial.print particular letters of a string

Bellow is a very basic sketch that prints what ever I type on the serial monitor.

Lets say hypothetically I typed “hello” into the serial monitor, is there a way I could alter this sketch to only print ether the “h” or the “E”

void setup()
{ Serial.begin(9600);
}

void loop()
{
String msg = “”;
if(Serial.available()>0)
{

while(Serial.available()>0)
{
msg += char(Serial.read());
delay(250);

}
Serial.println(msg);
}

}

serialPrint_Sketch.ino (266 Bytes)

Serial.println(msg[0]);

will print just one character - the one whose index you specified in the [].

Thanks!

So the msg[] worked great... How can I combine characters i.e something like

combined = msg[0] + msg[1]

Serial.print(combined)

and "He" is printed on the serial monitor

Mpc1055: So the msg[] worked great... How can I combine characters i.e something like

combined = msg[0] + msg[1]

Serial.print(combined)

and "He" is printed on the serial monitor

Well, the quick-and-easy answer would be

Serial.println(msg.substring(0, 2));

However, by making intensive use of String objects here we are really abusing this heavy and resource-hungry class.

A better idea would be

char sub[3] = {};
memcpy(sub, msg.c_str(), 2);
Serial.println(sub);

Thanks for the insight! Of course I am now more confused than ever, but this gives me something to start dissecting and learn more about. Thanks again for the help