Question regarding substring. Please Help :(

Hi, I wanted to split string accordingly before sending. I am learning about substring from this video Arduino Tutorial # 4.5: Better Serial Parsing - YouTube . I wanted to split string +IPD,0,2:#1.1.1s by just extracting #1.1.1s\n, so based on the video, my part1= com.substring(0, com.indexOf("#")); while part 2 = com.substring(com.indexOf("#"), com.indexOf(" "));

Here's my code:

#define esp8266 Serial3

String command;
long int previousMillis=0, currentMillis;

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

void loop()
{
currentMillis = millis();
if( (currentMillis - previousMillis) > 500)
{
esp8266.print("+IPD,0,2:#1.1.1s\n");
previousMillis = currentMillis;
}

if(Serial.available())
{

char c = Serial.read();
command += c;
parseCommand(command);
command = "";
}
}

void parseCommand(String com)
{
String part1;
String part2;

//TAKE A LOOK FROM HERE!! HELP :frowning:

part1= com.substring(0, com.indexOf("#"));
part2 = com.substring(com.indexOf("#"), com.indexOf(" "));

Serial.print(part2);
}

I printed out part2 and it printed out # only. I thought should be #1.1.1s since the video tutorial mention that com.substring(from, up to) ?
Really frustrated. Please help :frowning:

Stop investing time using the String class - you'll get frustration later on with possibly memory issues

if your buffer holds char cmd[] = "+IPD,0,2:#1.1.1s\n" and you want the final part then the pointer to the string you are interested in is just (cmd+10) if the format is fixed

const char msg[] = "+IPD,0,2:#1.1.1s\n";

void setup()
{
  Serial.begin(115200);
  Serial.println(msg);
  Serial.println(msg + 10); // if format is really known, start at position 10
  Serial.println(msg + (strlen(msg) - 7)); // to print the last 7 chars
  Serial.println(strchr(msg, '#')+1); // to print from 1st char after the #
}

void loop() {}

will print out to your console

+IPD,0,2:#1.1.1s

1.1.1s

1.1.1s

1.1.1s

things worth looking at:

stdlib.h
string.h
Serial Input Basics

Hi J-M-L

Thanks so much for replying.

The "+IPD,0,2:#1.1.1s\n" is coming from another serial (another arduino) through UART communication.
So it need to go through Serial.read()

So, const char c[] = Serial.read() ?
Then Serial.println(c);
Serial.println(msg +10);
and so on?

well build the cmd buffer by reading from your secondary Serial until your read the \n

for that read the Serial Input Basics link from previous answer - it also shows how to parse for content

The Serial object also knows how to perform a few basic stuff, like find, findUntil, readBytesUntil etc.... but it's a great exercise to implement for yourself

if I were you, I'd read the Serial input and drop the chars until I get the '#', then read and save the incoming chars one after the other in the cmd buffer until I get the '\n'.

--> live parsing as things come in

really read the Serial Input Basics link, it will all become super clear :slight_smile:

Dear J-M-L,

My problem is solved. Thank you so much for taking some time to reply my post and introduce me many useful references. I truly appreciate it. I thank you from the bottom of my heart and God bless you :slight_smile:

great - with pleasure!