Passing data from string to another string

Hi all
I have a small code here:

String response = "Night_Time";
String Time = "";
int i, flag;
for(i = 0; i < response.length(); i++)
{
    if(response[i] == '_')
  {
    flag = 1;
  }
    if(flag == 1)
  {
  Time[i] = response[i];
  }
}

The purpose of my code here is to copy the data from String "response" to String "Time" when the condition is met. I do it the same as the way we copy the data in the array. However, it seems like it does not work like that since I print the string "Time" to serial, it returns nothing

need a different var to keep track of the placement into Time[], can't use i..

good luck.. ~

time has no size??
~q

you can search for text in a String object using indexOf

if(response.indexOf("Time") >=0) {
  // process time

You're missing some stuff from your code but that's ok I added it for you.

Is this what you're hoping to accomplish in the bigger picture? Note, set Serial Monitor to 115200 baud.

String response = "Night_Time";
String Time = "";
int flag = 0;

void setup() {
  Serial.begin(115200);
}
void loop() {
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == '_') {
      flag = 1;
    }
    if (flag == 1)
    {
      Time = response;
      Serial.println(Time);
      flag = 0;
    }
  }
}

i was thinking..

String response = "Night_Time";
String Time = "        ";
int i, j, flag;
for(i = 0; i < response.length(); i++)
{
    if(response[i] == '_')
  {
    flag = 1;
    j=0;
  }
    if(flag == 1)
  {
  Time[j] = response[i];
  j++;
  }
}

should do it too..

~q

1 Like

Time is an empty String. its length is 0.

When you look at how the operator [] is implemented, you'll see that you get a bogus variable to write into if you try to write behind the length of the array.

so you are never storing anything into Time


What is it you are really trying to achieve ? if response contains '_' then duplicate response into the Time variable?

➜ You can copy one String into another one if you do

 Time = response;

You just need to do that when the condition is met.

Yeah, I know..
Like Arduino reference too..
language/variables/data-types/stringobject
Harder to get too nowadays though.. :frowning:

I tend to stay away from Strings() on these little chips..

You're a talented developer I value your intake, thanks..

~q

Here's another way that you could do what I think you want (take some Serial input and transpose pre defined Strings into a Time String). You could make as many as you have chars available.

String responseA = "Night_Time";
String responseB = "Day_Time";
String Time = "";

void setup() {
  Serial.begin(115200);
}
void loop() {
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    switch (inByte) {
      case '_':
        Time = responseA;
        break;
      case '?':
        Time = responseB;
        break;
    } // end of switch
    Serial.println(Time);
  } // end of if (Serial.available)
}


Use the SafeString-library.
Strings can eat up all RAM memory over time and then make the code crash
The Name SafeString is program: safe to use
And in your case more comfortable

// https://wokwi.com/projects/396975399677155329
// https://forum.arduino.cc/t/passing-data-from-string-to-another-string/1255815
#include <SafeString.h>

cSF(response, 16); // SafeString that can hold 16 characters
cSF(Time, 16);

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  byte i;
  byte j;
  byte flag = 0; // initialise with ZERO !!
  response = "Night_Time";
  Time = "";

  for (i = 0; i < response.length(); i++) {
    Serial.print("response[");
    Serial.print(i);
    Serial.print("]=#");
    Serial.print(response[i]);
    Serial.println("#");

    if (response[i] == '_') {
      Serial.println("found the _");
      flag = 1;
      j = 0;
    }

    if (flag == 1) {
      if (response[i] != '_') {
        Time += response[i];
        Serial.print("Time[");
        Serial.print(j);
        Serial.print("]=#");
        Serial.print(Time[j]);
        Serial.println("#");
        j++;
      }
    }
    Serial.print("Time=#");
    Serial.print(Time);
    Serial.println("#");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}
1 Like

or like @horace mentions..
could replace it all with..

  String Time = response.substring(response.indexOf('_'));

no loops needed..

happy coding.. ~q

that's way too complicated for the use case..

You can just use then simple cString functions (the safe version)

char responseStr[16] = "Night_Time";
char timeStr[16] = "";

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

  char * underscorePtr = strchr(responseStr, '_');
  if (underscorePtr != nullptr) {
    strlcpy(timeStr, underscorePtr + 1, sizeof timeStr);
    Serial.print("Found underscore, rest of text is [");
    Serial.print(timeStr);
    Serial.println("]");
  } else {
    Serial.print("Could not find any underscore in [");
    Serial.print(responseStr);
    Serial.println("]");
  }
}

void loop() {}

@pepernamek1 - "time" is a reserved word. "Time" is not a reserved word, but could cause confusion later. @J-M-L shows how to incorporate the word "time" into a safe variable name.

1 Like

The above is a ridiculous construct that is exactly equivalent to:

output

Night Time
Day Time
By the night of the shining moon
String responseA = "Night_Time";
String responseB = "Day_Time";
String responseC = "By the night of the shining moon";
String Time = "";

void
copy (
    String str )
{
    char     time [90];
    unsigned i;

    for (i = 0; i < str.length(); i++) {
        char c = str[i];
        if (c == '_')
            c = ' ';
        time[i] = c;
    }
    time [i] = '\0';        // nul terminator
    Serial.println (time);
}

void setup()
{
    Serial.begin(115200);
    copy (responseA);
    copy (responseB);
    copy (responseC);
}

void loop() {
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.