在C++中如何在字符串中查找int
In C++ how to find an int in a string
如果可能的话,我想这样做,
char buff[]="MA A8C : B12A : D14C: ... etc."
int myId=12;
if (myId exist in Buff){print i found it}else{it doesn't not exist}
所以简单地说,我想表明这个整数确实存在于这个字符串中,我不需要位置,也不需要它被提到的位置或次数,或者任何复杂的事情,我所有的搜索都会导致有人试图找到这个int的位置。。。但在我的情况下,我只想找到它是不是有回报1不在那里?返回0。感谢
你不能直接这么做。您可以先将数字转换为字符串,然后在buff
中搜索该字符串。
你可以使用C方法:
char temp[10];
sprintf(temp, "%d", myId);
if ( strstr(buff, temp) != NULL )
{
// Found it.
}
else
{
// Did not find it.
}
如果你想使用更多的C++方法,你可以使用:
auto found = std::string(buff).find(std::to_string(myID));
if ( found != std::string::npos )
{
// Found it.
}
else
{
// Did not find it.
}
相关文章:
- 如何从map<pair<string,int>,pair<string,Array>>中迭代和查找?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- 在 c++ 中的 multiset<pair<int,int> > 中查找下限
- 从两个或多个不同的动态数组中查找缺失的 int 值
- 如何在int数组中查找用户定义元素的数量
- 查找最大的无符号 int ....为什么这不起作用?
- 查找存储在长位中的最后一个int(1-9)(每个int由4位表示)
- 使用在地图上查找<对,int>
- std::set<pair<int,int> > -- 仅使用 pair<>::first 作为键查找
- 如何在将多维 int 数组映射到 C++ 中的字符串时实现在地图上查找或计数
- unique_ptr<int[]>、vector <int>和 int[] 的模板化类型查找
- 在未排序的向量<int>上查找
- 查找数组中第K个最大的int
- 在C++中如何在字符串中查找int
- 在 int 变量中查找 '1 数的奇偶校验
- 在线性搜索中查找int类型的最后一次出现
- c++ int数组指针递归查找质数因子
- 如何查找数组的大小作为一个int
- 如何在我的程序中查找"const char* + int"表达式
- 使用递归在List中查找int类型-何时返回