Removing keywords from a string

hey, ive got a pretty basic issue here, im trying to remove the chars '<' and '>' and the text between them.
the code below is my attempt at this, but instead of serial printing "theres a really ugly tag before this, this, this" i get "theres a really ugly tag before this"

an image of my serial monitor. the text printed first is the un-edited string, and the one after is an attempt to remove text between the '<' and '>'

String payload = "<tag>theres an ugly tag before this,<bigtag>this,<reallybigtag>this,";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
tagstrip(payload);


Serial.println(" ");
   Serial.println(" ");
  Serial.println(payload);
}

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

}
int howmuch = 0;
void tagstrip(String mystring){
for(int i = 0; i < mystring.length();i++){
int tagend = -1;
int tagstart = -1;
tagstart = mystring.indexOf('<');
tagend = mystring.indexOf('>');

 mystring.remove(tagstart,tagend - tagstart);
payload = mystring;
}
}

this is how I did it.

void fParseLIDAR_ReceivedSerial ( void * parameters )
{
  // distribute received LIDAR info
  String sTmp = "";
  sTmp.reserve ( 20 );
  String sMessage = "";
  sMessage.reserve ( StringBufferSize300 );
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
    xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
    //        Serial.println ( sMessage );
    int commaIndex = sMessage.indexOf(',');
    sTmp.concat ( sMessage.substring(0, commaIndex) );
    sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
    //    Serial.println ( sTmp );
    if ( sTmp == "!" )
    {
      xSemaphoreGive ( sema_LIDAR_OK );
      //  Display info from LIDAR
      sLIDAR_Display_Info = sMessage;
    }
    if ( sTmp == "$" )
    {
      xEventGroupSetBits( eg1, evtResetWatchDogVariables );
    }
    if ( sTmp == "#")
    {
      // Serial.println ( "#" );
      xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
      sLIDAR_Alarm_info = sMessage;
      xSemaphoreGive( sema_LIDAR_Alarm );
      xEventGroupSetBits( eg, evtfLIDAR_Alarm );
    }
    //    Serial.println ( "parse serial ok" );
    sTmp = "";
    sMessage = "";
    xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
    //    Serial.print( "fParseReceivedSerial " );
    //    Serial.print(uxTaskGetStackHighWaterMark( NULL ));
    //    Serial.println();
    //    Serial.flush();
  }
  vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )

hope it helps.

hmm, looks very complicated, ill see if i can modify it for my purpose

Note: Uses function return instead of writing into a global variable.

String payload = "<tag>theres an ugly tag before this,<bigtag>this,<reallybigtag>this,";

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  payload = tagstrip(payload);

  Serial.println();
  Serial.println();
  Serial.println(payload);
}

void loop(){}

String tagstrip(String mystring)
{
  int tagLevel = 0;
  String outputString;

  for (unsigned i = 0; i < mystring.length(); i++)
  {
    if (mystring[i] == '<')
    {
      tagLevel++;
    }
    else if (mystring[i] == '>' && tagLevel > 0)
    {
      tagLevel--;
    }
    else if (tagLevel == 0)
    {
      outputString += mystring[i];
    }
  }
  return outputString;
}

Or with c-strings : WBMwt2 - Online C++0x Compiler & Debugging Tool - Ideone.com

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