如何查找字符串中包含破折号的字符串
How to find string that contain dash in a string
我尝试用这个代码获得字符串"-a"的位置
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}
产生如下输出:
position found = 4294967295
如果我删除'-',它的工作如预期。返回8。
返回string::npos
值,因为库认为它在字符串中找不到-a
。
这样做的原因是您使用不同的破折号-在字符串中使用长破折号–
,在搜索字符串中使用短破折号-
。
一旦你在两个地方用正确的字符替换,你的代码开始正常工作(演示)。
表示有不同的第一个字符
您可以使用第一个字符并将它们放在语句
中进行检查。std::cout << ( '–' == '-' ) << std::endl;
由于它们是不同的函数find返回值std::string::npos
,该值定义为std::string::size_type( -1 )
或等于4294967295
如果你仔细看,你会发现'-'不是'-'。
相关文章:
- 编译包含字符串的代码时遇到问题
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- C++ 创建包含链表和字符串的对象的链接列表时出错
- C++-字符串是否包含一个带有简单循环的单词
- 编辑字符串以包含括号 c++ 之间的变量
- 使用提升元解析字符串解析包含的文件
- 当字符串包含 %2C 时,在 c++ 中不完整的 std::string
- C++ 检查 UTF8 字符串包含指定的字符
- 将文件作为字符串包含在内
- 为什么string.empty()在字符串包含一个值时评估为true
- boost::p rogram_option::store 在选项字符串包含混合语言字符时引发异常
- 在 c++ 中将字符串(包含字符和浮点数)转换为双精度
- 将字符串(包含数字)转换为整数并返回该整数
- 如果输入字符串包含"%",则vsntprintf_s崩溃
- 当一个字符串包含另一个字符串的一部分时比较字符串
- 如果字符串包含多个 ,如何在每25个换行符( )上分割字符串
- 输出字符串包含奇怪的字符
- 当字符串包含多个NUL字符时,如何获取c_str(用于发送)
- 乘法的字符串[包含整数],输出也存储在字符串中,如何