How to store String in EEPROM of Arduino...????

I am an Arduino newbie and I am having trouble storing String in Arduino Library.... Any help on how to do this would be a life saver for me.
This is a glimpse of method I am trying to implement....!!!

#include<EEPROM.h>

String mystr[]="Hello World";

void setup() {
// put your setup code here, to run once:

Serial.begin(115200);

for(int i=0;i<12;i++)
{

EEPROM.write(i,mystr*);*

  • }*

}
void loop() {

  • // put your main code here, to run repeatedly:*

}

Almost there:

You do not need an array of strings, just one.
Also, you need to access each element/character using the subscript operator: 'mystr[i]'.

#include<EEPROM.h>

String mystr="Hello World";

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

  for(int i=0 ; i < mystr.length() ; i++)
  {
    EEPROM.write( i,mystr[i] );
  }
}

void loop() {
}

However your example could also be done using a 'char' array, this method is preferred as the String class uses slow and limited dynamic memory.

#include<EEPROM.h>

char mystr[] = "Hello World";

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

  for(int i=0 ; i < sizeof(mystr) ; i++)
  {
    EEPROM.write( i,mystr[i] );
  }
}

void loop() {
}
#include<EEPROM.h>

String mystr="Hello World";

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

  for(int i=0 ; i < mystr.length() ; i++)
  {
    EEPROM.write( i,mystr[i] );
  }
}

void loop() {
}

It is very useful for me! Thank you!

And how can I read this String?

Maybe this?

#include<EEPROM.h>
String mystr;
char f[] = "";

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

void loop() {
    for(int i=0 ; i < 20 ; i++){  //you have to know broadly the lenght of the expected string
      f[i] = EEPROM.read(i);
    }
      if (mystr.length() < 20){ 
        mystr += (f);
      }
  Serial.println(mystr);
  delay(500);
}

Maybe this?

NO!

That code creates an array of length 1, because the only initializer provided is the NULL that terminates the (empty) string. Then, it tries to write 20 characters to the 20 elements of the 1 element array.

One could piss away memory uselessly using a String object like so:

   String wasteOfResources;
   for(byte b=0; b<whoKnowsHowManyCharactersWereStored(); b++)
   {
       wasteOfResources += EEPROM.read(b);
   }

Be sure to define that function, though.

OP, You MUST store the length of the string that the useless String instance wraps in EEPROM, too, so that you have some clue what whoKnowsHowManyCharactersWereStored() should return.

You helped me!

Thanks!

One real solution for reading the saved String is that:

         String IPis;
          for(byte b=0 ; b < 15 ; b++){  //15 is the maximum lenght of an IP adress with dots
            char f = EEPROM.read(b);
            IPis += (f);
          }

I run the for cycle 15 times, because I need to run 15 times. But you can change if you want to read a shorter or a longer String. The "char f" is needed because the String is stored in the EEPROM in ASCII format. And if you want to read like above PaulS it will results ASCII numbers in decimal values. Arduino Reference - Arduino Reference