SubString Function Not Working in ESP32

To parse out data received, I use "SourceStr.substring(Start, End - Start)" but get nothing even close to expected results. Made a test script to parse three different strings. One with numbers and commas, substring get close but shorts the substring by 3 characters. One with letters and commas, substring get close but shorts the substring by 3 characters. One with any keyboard character, the substring looks like most of the original string.

Below is the script being run on a ESP32 Deneyap Mini v2 development board. Also tried it with the same results on a ESP32 LOLIN S2 Mini

/*
* To test SourceStr.substring(Start, End - Start)
*
*/

//String PanTilt = "a,b,c,d,e,f,g,h,i,j,k,m";
//String PanTilt = "1,2,/n/r3,4/n/r,5,6,7,8,9,10,11,12";
String PanTilt = "POST / HTTP/1.1\r\nHost: 192.168.0.3\r\nConnection: Keep-Alive\r\nContent-Length: 24\r\n\r\nPanTilt_Az,numSteps_100;\r\n";
String numSteps;
int Count = 0;

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

void loop() {
  while(Count<4){
  //numSteps = getBetween(PanTilt, "b", "f");
  //numSteps = getBetween(PanTilt, "2", ",6");
  numSteps = getBetween(PanTilt, "PanTilt_", ",numSteps_");
  Serial.print("numSteps=  ");
  Serial.println(numSteps);
  delay(2000);
  Count = Count +1;
  }
}

String getBetween(String SourceStr, String strStart, String strEnd) {
  int Start = 0;
  int End = 0;
  //String SourceStr = strSource;
  Serial.print("SourceStr= ");
  Serial.println(SourceStr);
  Serial.print("SourceStr.indexOf(strStart)= ");
  Serial.println(SourceStr.indexOf(strStart));
  Serial.print("SourceStr.indexOf(strEnd)= ");
  Serial.println(SourceStr.indexOf(strEnd));
  Serial.print("SourceStr.length()= ");
  Serial.println(SourceStr.length());


  if (SourceStr.indexOf(strStart) > -1 && SourceStr.indexOf(strEnd) > -1) {

    Start = SourceStr.indexOf(strStart, 0) + strStart.length();
    End = SourceStr.indexOf(strEnd, Start);

    Serial.print("Start= ");
    Serial.print(Start);
    Serial.print(", End= ");
    Serial.println(End);
    Serial.println(SourceStr.substring(Start, End - Start));

    return SourceStr.substring(Start, End - Start);
  } else {
    return "";
  }
}

The following is the serial monitor output for one pass of the loop

08:09:16.892 -> SourceStr= POST / HTTP/1.1

08:09:16.892 -> Host: 192.168.0.3

08:09:16.892 -> Connection: Keep-Alive

08:09:16.892 -> Content-Length: 24

08:09:16.892 ->

08:09:16.892 -> PanTilt_Az,numSteps_100;

08:09:16.892 ->

08:09:16.892 -> SourceStr.indexOf(strStart)= 82

08:09:16.892 -> SourceStr.indexOf(strEnd)= 92

08:09:16.892 -> SourceStr.length()= 108

08:09:16.892 -> Start= 90, End= 92

08:09:16.892 -> ST / HTTP/1.1

08:09:16.892 -> Host: 192.168.0.3

08:09:16.892 -> Connection: Keep-Alive

08:09:16.892 -> Content-Length: 24

08:09:16.892 ->

08:09:16.892 -> PanTilt_

08:09:16.892 -> numSteps= ST / HTTP/1.1

08:09:16.892 -> Host: 192.168.0.3

08:09:16.892 -> Connection: Keep-Alive

08:09:16.892 -> Content-Length: 24

08:09:16.892 ->

08:09:16.892 -> PanTilt_

Wrote C# console code in VS2022 to be similar to the ESP32 script and it parse all three strings correctly.

Any help would be appreciated.

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

The second parameter is the index of the first character you don’t want included (it’s not the length of the substring)

So if you want the text in between the two search strings, excluding those Strings, the first param is the index of the start string plus its length and the second one is directly the index of the end string.


Doc states

Description

Get a substring of a String. The starting index is inclusive (the corresponding character is included in the substring), but the optional ending index is exclusive (the corresponding character is not included in the substring). If the ending index is omitted, the substring continues to the end of the String.

Syntax

myString.substring(from)
myString.substring(from, to)

Parameters

myString: a variable of type String.
from: the index to start the substring at.
to (optional): the index to end the substring before.

Thanks, that worked. I follow the Microsoft documentation which is;

Substring(Int32, Int32)

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=net-9.0

What documentation should I use in the future?

The Arduino documentation when it comes to arduino specific classes like String (it is not the same as std::string)

(I would also use the general c++ doc either https://cplusplus.com/ or cppreference.com rather than Microsoft’s docs)

1 Like

Thanks :slightly_smiling_face:

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