-1

I work in Qt Creator (Community) 5.5.1. For example, I have

string="44° 36' 14.2\" N, 33° 30' 58.6\" E, 0m"

of QString. I know, that I must parse it, but i don't know how, because I have never faced with the problem like it. From our string I want to get some other smaller strings:

cgt = "44"; cmt = "36"; cst = "14.2"

cgg = "33"; cmg = "30"; csg = "58.6"

What must I do for working my programm how I said?
I need real code. Thanks.

Khan
  • 81
  • 1
  • 5

2 Answers2

1

The simplest way to start would be string.split(' ') - that would yield the list of the string components that were separated by the space character (' '). If you're sure the string will always be formatted exactly like this, you can first remove all the special characters (° and so on).

Then analyze the resulting QStringList. Again, if the format is fixed, you can check that the number of list items matches the expected number, and then get degrees as list[0], minutes as ``list[1]` and so on.

Another alternative would be to use QRegExp for parsing the string (splitting it into substrings based on regex), but I find it too complicated for use cases where split works just as well.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
0

"I need code" is not the kind of question you should be asking, SO is about "gimme knowledge" not about "do my work" questions. A good question should demonstrate your effort to solve the problem, so people can tell you what you are doing wrong. Not only does your question lack any such effort, but you didn't expend any even when Devopia did half of the work for you. Keep that in mind for your future questions.

struct G {
    double cgt, cmt, cst, cgg, cmg, csg;
};

G parse(QString s) {
    QStringList list = s.split(QRegExp("[^0-9.]"), QString::SkipEmptyParts);
    G g;
    g.cgt = list.at(0).toDouble();
    g.cmt = list.at(1).toDouble();
    g.cst = list.at(2).toDouble();
    g.cgg = list.at(3).toDouble();
    g.cmg = list.at(4).toDouble();
    g.csg = list.at(5).toDouble();
    return g;
}
dtech
  • 47,916
  • 17
  • 112
  • 190
  • Thank you! I know. You can see my previous questions, where is effort. But here I didn't find any usable information. Sorry. – Khan Jan 16 '16 at 19:55