Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查字符串是否包含字符?_C++_Stdstring - Fatal编程技术网

C++ 如何检查字符串是否包含字符?

C++ 如何检查字符串是否包含字符?,c++,stdstring,C++,Stdstring,我有一个我想读的文本文件。我想知道其中一行是否包含[,因此我尝试: if(array[i] == "[") 但这不起作用 如何检查字符串是否包含特定字符?查看文档 如果数组是char*array或char array[],则可以通过while循环找到char: while(i < nSize) if (array[i] == '[') while(i

我有一个我想读的文本文件。我想知道其中一行是否包含
[
,因此我尝试:

if(array[i] == "[")
但这不起作用

如何检查字符串是否包含特定字符?

查看文档


如果数组是
char*array
char array[]
,则可以通过
while
循环找到
char

while(i < nSize)
    if (array[i] == '[')
while(i

注意:
'['
是正确的字符文字,但
“[”
是字符串文字。

要检查字符串中的字符是否等于特定字符,需要按以下方式进行检查:

if(array.at(i) == '[') {
}
有关更多详细信息,请查看以下链接:
)

在字符串中,我们可以使用
find()
获取给定“字符串”的第一个匹配项(位置)

string s=“dumm[y[”;
int found=s.find('[');
我是这样做的

string s=“更多+”;

如果(s.find(+')从C++23开始,您可以使用

#包括
常量自动测试=标准::字符串(“测试”);
if(test.contains('s'))
{
//找到了!
}

@jamek
'['
将是一个字符文字,
“[”
是一个c字符串。什么是
数组
字符*
字符[]
std::string
vector
,我认为这些容器中的任何一个都属于这个问题的范畴。@JabberwockyHi@Meraj。这些标记都很好。添加
string
char
没有任何用处。没有人搜索
char
。谢谢。或者仅仅使用这些代码不会编译!即使你已经将
firstOccurrence
替换为
found
并将
str
替换为
s
find()
将返回4,而不是3。在此处发布答案之前,请至少运行您的代码并确保其正确无误,谢谢!@SebastianWilke感谢您的更新。编辑了我所犯的错误。我将在发布之前检查我的代码,不再重复。什么是npos?这是您试图匹配字符的字符串的位置吗r with?@AdanVivero,如果您阅读了文档的返回值部分,
npos
是在未找到此类子字符串时返回的值。
if(array.at(i) == '[') {
}
string s = "dumm[y[";
int found = s.find('[');

cout<<"$ is present at position "<<firstOccurrence;   //$ is present at position 4

if (found < str.length()) {
    // char found
}
else{
    // char not found
}
#include <string>

const auto test = std::string("test");

if (test.contains('s'))
{
    // found!
}