简体   繁体   English

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

[英]C++ Find last char in char array

Basically I'm trying to find last char in a char* array. 基本上,我试图在char *数组中找到最后一个char。 eg; 例如; I have this string: 我有这个字符串:

"This is a long string with many other \"characters\". Hehehe"

I tried to use this: 我试图用这个:

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;
}

Like this: 像这样:

int lastCharPos = findLast ( charArray, ftell(file), '"', charSize );

In the charArray we have: 在charArray中,我们有:

"This is a long string with many other \"characters\". Hehehe"

findLastChar returns the pos of " that comes after "characters", but the last one should be after Hehehe. findLastChar返回“”的位置,该位置在“字符”之后,但最后一个应该在呵呵之后。

The idea is to return the position of the last "char" specified by user request, however it doesn't work. 这个想法是返回用户请求指定的最后一个“字符”的位置,但是它不起作用。

I'm trying to use it with this string: 我正在尝试将此字符串与它一起使用:

"Welcome to Stackoverflow!\nWe seriously hope you have a \"great\" time."

It returns (the code says the " after great is the last one, but as you can see in the given string, it's not): 它返回(代码说“ after after是最后一个,但是正如您在给定字符串中看到的那样,不是)”:

"Welcome to Stackoverflow!\nWe seriously hope you have a \"great\"

Trying to use it to search for " 尝试使用它来搜索“

this is tagged c++, so dont use char* 这被标记为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