Usage of String.replace()

I'm trying to print a page I made in html via Arduino ethernet.
I splitted this page in 15 files.
If I print every single one without changing data, the result is exactly what I wanted.
But I need to change values inside an input putting some values I have in Arduino's memory, so I tried doing this using a

String.replace("value=\"\"/>", "value=\"DataIWantToPutInHere\"/>")

every time a line is printed, but the result in the browser is totally different...
This is the code:

#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>

const char* DATASTRING = "DATASTARTSHERE";
const char* HOURSETUP = "HOUR";

bool hour_selected = false;
byte mac[] = { 0x**, 0x**, 0x**, 0x**, 0x**, 0x** };
IPAddress ip(***, ***, *, **);
EthernetServer server(**);

char*  vector[14];
int hours[28] = {12,0,12,2, 12,0,12,2, 12,0,12,2, 12,0,12,2,
                 12,0,12,2, 12,0,12,2, 12,0,12,2};

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  if(SD.begin(4)){
    Serial.println("SD initialized succesfully.");
  }else{
    Serial.println("Error while initializing SD.");
    while(1);
  }
}

void loop() {
  EthernetClient client = server.available();
  if(client){
    char c;
    String http_message;
    String message = "";
    Serial.println("New client");
    while(client.connected()){
      if(client.available()){
        c = client.read();
        message += c;
        if(c == '\n'){
          int cmd = message.indexOf(DATASTRING);
          Serial.println(message);
          if(cmd > 0){
            char* msg = message.c_str();
            char actChar = msg[cmd + 14];
            char* vals;
            int count = 0;
            String values = "";
            char* temp;
            while(actChar != 'D'){
              values += actChar;
              count++;
              actChar = msg[cmd + 14 + count];
            }
            vals = values.c_str();
            Serial.println(values);
            Serial.println("Now I print random strtoks:");
            count = 0;
            for(temp = strtok(vals, ";"); temp != NULL; temp = strtok(NULL, ";")){
              Serial.println(temp);
              vector[count] = temp;
              count++;
            }
            for(int i = 0; i < 14; i++){
              temp = strtok(vector[i], ":");
              hours[i*2] = atoi(temp);
              Serial.println(hours[i*2]);
              temp = strtok(NULL, ":");
              hours[i*2 + 1] = atoi(temp);
              Serial.println(hours[i*2 + 1]);
            }
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("I received data.");
            client.println("</html>");
            break;
          }else{
            char c;
            String tab;
            String str = "";
            File file; 
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            for(int i = 0; i < 15; i++){
              if(i < 10){
                tab = "tab";
                tab += i;
              }else{
                tab = "tac";
                tab += i -10;
              }
              tab += ".txt";
              Serial.print("Trying to open ");
              Serial.println(tab);
              file = SD.open(tab, FILE_READ);
              if(file){
                Serial.println("Opened.");
              }else{
                Serial.println("Error opening file.");
              }
              str = "";
              while(file.available()){
                c = file.read();
                if(c == '\n'){
//                  if(i < 14){
//                  String toRep = "value=\"";
//                    toRep += hours[i*2];
//                    toRep += ":";
//                    toRep += hours[i*2+1];
//                    toRep += "\"/>";
//                    str.replace("value=\"\"/>", "value=\"12:32\"/>");
//                  }
                  client.print(str);
                  str = "";
                }else{
                  str += c;
                }
              }
              file.close();
            }
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
    Serial.println("Client disconnected");
  }
}

The commented lines are the ones where I use String.replace()
Anyway, even putting String.replace("Value", "defaultValue") the page doesn't print all the , printing something like <input type="time" value=" which destroys all the page.

Bad news, I'm afraid.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

If you want to handle a lot of text on an Arduino you are even more likely to run into a problem with the String class.

...R

Do you run this sketch on an UNO? In this case you probably run out of memory. The UNO has 2kB of RAM and almost half of it is used for the SD subsystem. If you then use the String class (which is not made to be used on devices with that limited memory) you'll end having a totally fragmented memory which overruns and the behavior gets unpredictable. Learn to use C strings (char arrays) and take care to use as little memory as possible.

Robin2:
Bad news, I'm afraid.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

If you want to handle a lot of text on an Arduino you are even more likely to run into a problem with the String class.

...R

Yeah, I read it in This post, but i thought it was the only way to deal with an SD reading. I mean, with c strings I can't do something like

char* c = "";
c += 'a';

But, luckily, I saw that the function c_str() converts a String to a char*.
But the problem remains. I mean, how can I add those values in a char*?

pylon:
Do you run this sketch on an UNO? In this case you probably run out of memory. The UNO has 2kB of RAM and almost half of it is used for the SD subsystem. If you then use the String class (which is not made to be used on devices with that limited memory) you'll end having a totally fragmented memory which overruns and the behavior gets unpredictable. Learn to use C strings (char arrays) and take care to use as little memory as possible.

It's Arduino Ethernet, but it's similar, so I thought that was the problem too.

Deazard:
But the problem remains. I mean, how can I add those values in a char*?

cstrings are just arrays of the char type terminating with a 0.

If you define an array like this

char myTestArray[24] = {"bbb"};

then you can add a character in the 3rd position with

myTestArray[2] = 'a';

to get "bba"

Yo don't have to use the full size that is allocated to the array. Just put a 0 after the last character - in my example it will be after the third 'b'.

By defining the size of the array when you write the program you avoid problems of memory corruption.

There are lots of functions for manipulating cstrings in the link I gave you.

...R