2014-01-29 34 views
-1

我有以下代碼,使用Tesseract解決了一個小圖像。如何獲取char *的一部分?

char *answer = tess_api.GetUTF8Text(); 

我事先知道,結果總是以字符「+」開始,這是一個詞,所以我想擺脫它發現的任何垃圾。

我得到的結果爲「G + ABC S \ n \ n」,我只需要ABC。所以基本上我需要在+和第一個空格之後的所有內容之前忽略任何內容。我在想我應該使用rindex來查找+和空格的位置。

+1

你也許應該使用'的std :: string',它更容易。 – MSalters

+0

如果'tess_api.GetUTF8Text()'的結果是一個帶有uft8字符的字符串,那麼最好使用'wchar *'來代替'char *' – Narkha

+0

我期待的只是大寫字母。我不期待任何utf8字符,所以我使用了tess_api.SetVariable(「tessedit_char_whitelist」,「+ ABCDEFGHIJKLMNOPQRSTUVWXYZ」); – Crypto

回答

1

您可以使用:

// 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的答案)。

+0

結果可能有可變長度。在'+'之前可以有一個以上的字母,或者根本沒有,所以我需要從+到字的結尾(換行符或空格)的所有內容。 – Crypto

+1

@TonyD修正,謝謝。 – herohuyongtao

+0

@Crypto查看更新後的答案。 – herohuyongtao

3
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; 
} 
+0

不確定字符串'「G +」'是否是有效的輸入,但是會導致此算法出現問題。 – Lundin

+0

@Lundin你爲什麼這麼說?在第一個+ –

+0

之後搜索空間啊,實際上這不會是一個問題,而是它找不到這兩個符號中的任何一個。但是這個問題並沒有提到這種錯誤處理是必需的,所以從來不需要:) – Lundin