直接の答えは、おそらくstd::string::compare
またはstd::string::find
を使用している。私は、利点と欠点を持つ例に直接あなたを持っているだろう「C++の文字列で始まる」という用語でGoogle検索を想定
。
しかし、非常に単純なパーサーを書くときには、cの文字列ライブラリのトークン化ツールstrtok
を使用すると簡単になります。strtok
その後の分割各トークンはstrtok
への呼び出しによって次のトークンを返します。トークンを区切る文字をstrtok
にパラメータとして渡されます。
#include <iostream>
#include<stdio.h>
#include <stdlib.h>
int main()
{
std::string str = "type(a)";
if (str.compare(0,5,"type(") == 0) {
// string starts exactly with `type(`;
// note: 'type (' would not match...
}
char* analyzeStr = strdup(str.c_str());
char *token = strtok(analyzeStr, "(");
if (token && strncmp(token, "type", strlen("type")) == 0) {
// command starts with "type(" or "type ("
token = strtok(NULL, ")");
if (token) {
// value between brackets
printf("analyzing type of '%s'", token);
}
}
free(analyzeStr);
return 0;
}
http://stackoverflow.com/questions/931827/stdstring-comparison-check-whether-string-begins-with-another-string – drescherjm