Extract part of a String

Hey everybody,

I get my gps coordinates and I have this:5610.624229,N*01012.118300*,E,041216,190632.0,104.0,0,0

But in fact, I only would like what is in bold (latitude, longitude). Does someone know how to do this ? Maybe something with comma

THanks for your help Regards

Copy the first 28 characters into an array.
Make some decisions based on where the periods are - you may have 3, 4, 5 characters before the period, you will always have 6 after. Pick off the data based on where the peroids are.
You need the N and E info also, to indicate N or S (-) of the equator, and E or W (-) of the Prime Meridian.

In fact, I send data to xively, then parse it in android studio to display the current location on google map. Therefore I don't need the N or the E. What do you mean period ? How could I extract it outside from the arraylist ?

thanks

Welcome :slight_smile:

Shouldn’t there be a comma after N ? If so, try something like this: http://codepad.org/yNR4kv1G

yes there is one, it is because of the bold. :/ THanks I will try ;)

My code is not in C++ so I can't use it.. Sorry.. An other idea please ?

If you're not coding in C++, why are you posting in the Arduino forum?

I thought that it was C..

I tried what you did with your char and it works. Now, when I'm trying with this char it doesn't work anymore. Do you know why ?

String lat_long(){
    char str[] = "5610.624229,N,01012.118300,E,041216,190632.0,104.0,0,0";

    // store lat
    lat = strtok( NULL, "," );

    // skip 'N'
    strtok( NULL, "," );

    // store lon
    lon = strtok( NULL, "," );

    Serial.println( lat );
    Serial.println( lon );
}

Thanks

for (x=0; x<10; x=x+1){
lat[x] = str[x];
}
for (x=14; x<26; x=x+1){
lon[x-14] = str[x];
}

Thanks but the problem is that sometimes I can have a different str with more or less numbers for lat or lon so it couldn't work each time. It's why with strtok it worked well. Maybe I could see where is the comma each time and remember the length of it ?

Have a couple different cases, select which to use based on where the period(s) are.

   String gps = "5610.624229,N,01012.118300,E,041216,190632.0,104.0,0,0";
   int len = 35;
   char str[len];
   gps.toCharArray(str, len);
   Serial.println(str);

    char *lat, *lon;
    for (x=0; x<10; x=x+1){
    lat[x] = str[x];
    }
    for (x=14; x<26; x=x+1){
    lon[x-14] = str[x];
    }

    Serial.println( lat );
    Serial.println(lon);

I tried with your advice but it doesn’t work to write in lat or lon.

// store lat
lat = strtok( str, "," );

What do you get when you print?
I would do this:

    for (x=0; x<10; x=x+1){
    Serial.print (lat[x]);
    }
        Serial.println("");
    for (x=0; x<12; x=x+1){
    Serial.print (lon[x]);
    }
    Serial.println("");

Quantities of characters picked off may need some adjusting, I thought I counted them up correctly.

String lat_long(){
   //String gps = get_GPS();
   String gps = "5610.624229,N,01012.118300,E,041216,190632.0,104.0,0,0";
   int len = gps.length() + 1;
   char str[len];
   gps.toCharArray(str, len);
   Serial.println(str);

    char *lat, *lon;
    // store lat
    lat = strtok( str, "," );

    // skip 'N'
    strtok( NULL, "," );

    // store lon
    lon = strtok( NULL, "," );
    
    String latitude(lat);
    Serial.println("Latitude:" + latitude);
    String longitude(lon);
    Serial.println("Longitude:" + longitude);
    String coordinates = latitude +","+longitude;
    return coordinates;
}

With this code it works well! Thanks

That looks a lot cleaner than my method.

Stick to hardware... JK :)