Arduino substring doesn't work

I have a static method that searches (and returns) into String msg the value between a TAG

this is the code function:

static String genericCutterMessage(String TAG, String msg){
    Serial.print("a-----");
    Serial.println(msg);
    Serial.print("b-----");
    Serial.println(TAG);
    if(msg.indexOf(TAG) >= 0){
        Serial.print("msg ");
        Serial.println(msg);
        int startTx = msg.indexOf(TAG)+3;
        int endTx = msg.indexOf(TAG,startTx)-2;
        Serial.print("startTx ");
        Serial.println(startTx);
        Serial.print("endTx ");
        Serial.println(endTx);
        String newMsg = msg.substring(startTx,endTx);
        Serial.print("d-----");
        Serial.println(newMsg);
        Serial.println("END");
        Serial.println(newMsg.length());
        return newMsg;
    } else {
        Serial.println("d-----TAG NOT FOUND");
        return "";
    }
}

and this is output

a-----[HS][TS]5132[/TS][TO]5000[/TO][/HS]
b-----HS
msg [HS][TS]5132[/TS][TO]5000[/TO][/HS]
startTx 4
endTx 30
d-----
END
0
fake -_-'....go on!  <-- print out of genericCutterMessage

in that case I want return the string between HS tag, so my expected output is

[TS]5132[/TS][TO]5000[/TO]

but I don't know why I receive a void string.

to understand how substring works I just followed tutorial on official Arduino site

Any idea?

It's a lot easier for us if you post the entire program so we can run it ourselves.

It's also simpler to create a substring starting at a position to the end of the String, and then insert a NULL at the appropriate position, where the String should end. Determine THAT location (in the substring) AFTER extracting the first substring.

econjack:
It's a lot easier for us if you post the entire program so we can run it ourselves.

thanks for response

uhm It's difficult because it's a part of a Library called to another library, I should paste 6 or 7 files...tomorrow i'll create a little example program with this function

uhm It's difficult because it's a part of a Library called to another library, I should paste 6 or 7 files...

Then you should write code that will do the String parsing successfully on the character String you have, then integrate that back into your other code.

I don't know what the attraction of the string class is, is it really that convenient?

Here's a way of doing it with C style strings, untested obviously :wink:

  char tag[16];
  char msg[64];
  char new_string[64]; // Alternatively malloc this to the exact size of the substring ...

  char *start_tag_p = strstr( msg, tag );
  char *end_tag_p = NULL;
  bool tags_found = false;

  if ( start_tag_p )
  {
    // Found tag! ...
    end_tag_p = strstr( &msg[1], tag );
    if ( end_tag_p )
    {
      // Found the closing tag!
      int new_string_size = ( end_tag_p - start_tag_p );
      memcpy( new_string, start_tag_p, new_string_size );
      new_string[new_string_size + 1] = 0;
      tags_found = true;
    }
  }

  if ( !tags_found )
  {
    // Failed to find your substring ...
  }