Using Arduino String startsWith

Hi,

I hv this programming questions with String.startsWith() to match a string ( call ) with values already in a String called rxCallsign..

I do the following statement :-

...
char *call;
String rxCallsign;
...

// Do not add duplicated callsign
       if ( ! rxCallsign.startsWith(call) ) {
	     rxCallsign.concat(call);
        ....
        }

But the output still still shows the 8W2SSS-9 twice ....

8W2DDD-8/3.24km 8W2SSS-9/9.33km 8W2SSS-9/9.03km

Is there a better way to use String for searching a string ??

Thanks

Perhaps you should post all your code to understand what you are trying to do. Your capture method may leave things like carriage returns and line feeds in the serial buffer (if you are capturing serial data).

The call variable are part of a decoded packet without any CR/LF in it...

From the output below, I notice it was NOT working when the duplicated callsign is the 2nd in the list..
never the 1st callsign... and the code added the 3rd callsign.... (shd not add them)

Doesn't startsWith check all the way till the end of the string ???

@269/000/A=000369 Seq:36 9W2XXX-9/6.63km 9W2LLL-8/11.64km 9W2LLL-8/11.64km R

@269/000/A=000369 Seq:37 9W2SSS-9/6.63km 9W2LLL-8/11.22km 9W2LLL-8/11.22km R

@269/000/A=000369 Seq:38 9W2SSS-9/6.63km 9W2FFF-1/15.73km 9W2FFF-1/15.73km R

@269/000/A=000369 Seq:34 9W2VVV-1/10.04km 9W2SSS-9/6.63km 9W2SSS-9/6.63km R

void show_packet()
{
 char *posit, *pmsgTo, *call, *pcomment, *pmsg;
 char type, pmsgID;
 long lat, lon;
        static boolean nextLine = 0;

        // Only displasy if decode is true
 if ( microaprs.decode_posit(packet, &call, &type, &posit, &lon, &lat, &pcomment, &pmsgTo, &pmsg, &pmsgID) ) {

 if (type == 58) // 58 = "!" = Message
 {
 if (startsWith(MYCALL, pmsgTo))
 {
                    mCounter++;
            
                    // Beep 3 times
                    beep(3);
                    
                nextLine ^= 1 << 1;  // Toggle nextLine
 }
 }
 else // Not message, decode , calculate and display packets
 {   
            wayPointLatitude = lat; 
            wayPointLongitude = lon;

            wayPointLatitude = wayPointLatitude / 1000000;
    wayPointLongitude = wayPointLongitude / 1000000;

    distanceToWaypoint = calculateDistance();
    bearing = calculateBearing();
  
            // Beep twice is station is less than 500m
            if ( distanceToWaypoint < 0.5 ) {
                 beep(2);
            }
            
            // Check for valid decoded packets
            if ( strlen(call) < 12 ) {
            lastRx = millis();
            packetDecoded++;
            
            // Append rx callsign into rxCallsign strings
            // Do not add own callsign
            if ( !startsWith(MYCALL,call) ) {

                // Do not add duplicated callsign
         if ( !rxCallsign.startsWith(call) ) {
 rxCallsign.concat(call);
                        // Only send distance if GPS is locked AND less than 500km away
                        if ( gps.satellites.value() > 3 && distanceToWaypoint < 300 ) {
    rxCallsign.concat("/");
    rxCallsign.concat(distanceToWaypoint);
    rxCallsign.concat("km ");
                        } else {
                            rxCallsign.concat(" ");
                        }
 rxStation++;
                 }
    }



bool startsWith(const char *pre, const char *str)
{
 size_t lenpre = strlen(pre),
       lenstr = strlen(str);
 return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}

I tried to change the codes to startsWith OR endsWith:-

        	if ( !rxCallsign.startsWith(call) || !rxCallsign.endsWith(call)  ) {

It makes matter worst...

I notice it is matching the entire string together with the 7M2III-9/8.83km ...

Should I use substring instead ???

Output :-

@235/000/A=000440 Seq:0 7M2III-9/8.83km 7M2III-9/7.66km 7M2III-9/7.11km R

@235/000/A=000440 Seq:4 7W2VVV-9/10.48km 7W2VVV-9/10.49km 7W2VVV-9/10.47km R

Doesn't startsWith check all the way till the end of the string

I would think that would only happen if the Strings being compared were the same length. I think indexOf() will search the entire String looking for a match.