繁体   English   中英

C ++在char数组中查找最后一个char

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM