[英]C++ Find last char in char array
基本上,我試圖在char *數組中找到最后一個char。 例如; 我有這個字符串:
"This is a long string with many other \"characters\". Hehehe"
我試圖用這個:
int findLast (const char* buffer, int pos, char character, int size)
{
int last = 0;
for (int i = pos; i < size; i++)
{
if (buffer[i] == character)
last = i;
if (buffer[i] == '\n')
return last;
}
return last;
}
像這樣:
int lastCharPos = findLast ( charArray, ftell(file), '"', charSize );
在charArray中,我們有:
"This is a long string with many other \"characters\". Hehehe"
findLastChar返回“”的位置,該位置在“字符”之后,但最后一個應該在呵呵之后。
這個想法是返回用戶請求指定的最后一個“字符”的位置,但是它不起作用。
我正在嘗試將此字符串與它一起使用:
"Welcome to Stackoverflow!\nWe seriously hope you have a \"great\" time."
它返回(代碼說“ after after是最后一個,但是正如您在給定字符串中看到的那樣,不是)”:
"Welcome to Stackoverflow!\nWe seriously hope you have a \"great\"
嘗試使用它來搜索“
這被標記為c ++,所以不要使用char*
#include <string>
int findLastPos( std::string s, char c )
{
return s.rfind( string(1,c) );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.