我有以下代碼,使用Tesseract解決了一個小圖像。如何獲取char *的一部分?
char *answer = tess_api.GetUTF8Text();
我事先知道,結果總是以字符「+」開始,這是一個詞,所以我想擺脫它發現的任何垃圾。
我得到的結果爲「G + ABC S \ n \ n」,我只需要ABC。所以基本上我需要在+和第一個空格之後的所有內容之前忽略任何內容。我在想我應該使用rindex來查找+和空格的位置。
我有以下代碼,使用Tesseract解決了一個小圖像。如何獲取char *的一部分?
char *answer = tess_api.GetUTF8Text();
我事先知道,結果總是以字符「+」開始,這是一個詞,所以我想擺脫它發現的任何垃圾。
我得到的結果爲「G + ABC S \ n \ n」,我只需要ABC。所以基本上我需要在+和第一個空格之後的所有內容之前忽略任何內容。我在想我應該使用rindex來查找+和空格的位置。
您可以使用:
// just scan "answer" to find out where to start and where to end
int indexStart = // find the index of '+'
int indexEnd = // find the index before space
int length = indexEnd-indexStart+1;
char *dataYouWant = (char *) malloc(length+1); // result will be stored here
memcpy(dataYouWant, &answer[indexStart], length);
// for example answer = "G+ABC S\n\n"
dataYouWant[length] = '\0'; // dataYouWant will be "+ABC"
您可以爲其他的替代品退房Strings in c, how to get subString。
P.S.建議:使用string
而不是C++
,這將會容易得多(請查看@DavidSykes的答案)。
結果可能有可變長度。在'+'之前可以有一個以上的字母,或者根本沒有,所以我需要從+到字的結尾(換行符或空格)的所有內容。 – Crypto
@TonyD修正,謝謝。 – herohuyongtao
@Crypto查看更新後的答案。 – herohuyongtao
std::string ParseString(const std::string& s)
{
size_t plus = s.find_first_of('+');
size_t space = s.find_first_of(" \n", plus);
return s.substr(plus, space-plus);
}
int main()
{
std::cout << ParseString("G+ABC S\n\n").c_str() << std::endl;
std::cout << ParseString("G +ABC\ne\n").c_str() << std::endl;
return 0;
}
給人
+ABC
+ABC
如果你真的不能使用字符串,然後像這樣可能會做
char *ParseString2(char *s)
{
int plus,end;
for (plus = 0 ; s[plus] != '+' ; ++plus){}
for (end = plus ; s[end] != ' ' && s[end] != '\n' ; ++end){}
char *result = new char[end - plus + 1];
memcpy(result, s + plus, end - plus);
result[end - plus] = 0;
return result;
}
你也許應該使用'的std :: string',它更容易。 – MSalters
如果'tess_api.GetUTF8Text()'的結果是一個帶有uft8字符的字符串,那麼最好使用'wchar *'來代替'char *' – Narkha
我期待的只是大寫字母。我不期待任何utf8字符,所以我使用了tess_api.SetVariable(「tessedit_char_whitelist」,「+ ABCDEFGHIJKLMNOPQRSTUVWXYZ」); – Crypto