How to clear array of Strings?

String fruits[5];

fruits[0] = "apple";
fruits[1] = "kiwi";

How to clear array of strings?


for (int i=0; i<5; i++){
    fruits[i] = "";
}

Is it proper way?

and, Is there a way to test arduino code without connect arduino board?
I know there is some c++ online playground,
But I want to test Arduino's libraries also (like String, etc.)

See https://wokwi.com/

On an Uno/Mega I'd not do Strings. Instead use strings.

But is you insist on using Strings then take some precautions.

Here is this structure.

struct stu_message
{
  char payload [payloadSize] = {'\0'};
  String topic ;
} x_message;

A char array and a String ae used.

In setup(), I reserve a space of ram, a buffer, the string will use like so

  x_eData.WD.reserve(50);
  x_message.topic.reserve( payloadSize );

Here I declare a String, reserve memory space for the strig, and put stuff in it.

     String sTopic = "";
      sTopic.reserve( 35 );
      sTopic.concat( String(x_eData.SunRiseHr) + "," );
      sTopic.concat( String(x_eData.SunRiseMin) + "," );
      sTopic.concat( String(x_eData.SunSetHr) + "," );
      sTopic.concat( String(x_eData.SunSetMin) + "," );
      sTopic.concat( String(x_eData.DawnHr) + "," );
      sTopic.concat( String(x_eData.DawnMin) + "," );
      sTopic.concat( String(x_eData.TransitHr) + "," );
      sTopic.concat( String(x_eData.TransitMin) );

Here is how the String is cleared.

 sTopic = "";

By reserving a spot of RAM, a buffer, for the String, the String will not be making memory holes.

To insert info into the String buffer use xString.concat and "" to empty the String.

Yes, the way you have posted to clear a String array clears the String.