[solved] How can I select a fixed part of string?

Hello arduino fans :slight_smile:
I am working with GPS data. I just want to get the latitude and longitude data from a string. For example:
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
From the above string, I want to only get 4807.038,N,01131.000,E in an array like
array[0]= 4807.038
array[1]= N
array[2]= 01131.000
array[3]= E
N.B.: I don't want to use any complex libraries. :frowning:

if it's a c-string (as it ought to be, String class is not recommended), strtok() is the tool to use.

The parse example in Serial Input Basics illustrates the use of strtok()

...R

But I am getting this is in string format in my code. How can I convert them into char?

mahfuzasif:
But I am getting this is in string format in my code. How can I convert them into char?

I assume that you mean String (uppercase S) format. Show us your code.

Here is the partial code, I am receiving $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,*47 in the cont.

You posted an image of your code. That is useless. Please post your entire sketch, in code tags please.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the

[code]and [/code]

metatags.

When you are finished that, please read this post:

How to use this forum - please read.

First of all, apologizing for not following the forum rules. :frowning:

aarg:
You posted an image of your code. That is useless. Please post your entire sketch, in code tags please.

Secondly, thanks to you all. I have successfully done the parsing. :slight_smile: Here is the code:

char data[92];
  cont.toCharArray(data, 92) ; //cont holds the $GPGGA string
  
  char* pch;
  pch = strtok (data, ",");
  int i=0;
  while (pch != NULL){
    fields[i]=pch; //char fields[15];
    pch = strtok (NULL, ",");
    Serial.println(fields[i]);
    i++;
  }

Good news. If you like, you can also mark the title as "SOLVED".

mahfuzasif:
Here is the partial code, I am receiving $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47 in the cont.

So, cont is a String, but does it have to be ? Could it be a C style string ?