在字符串行中找到转义字符 [英] Find escaped characters in a string line

查看:174
本文介绍了在字符串行中找到转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用std库和函数 find ,我如何知道给定字符串中是否有转义字符?

Using the std library, and the function find, how can I know if there are any escaped characters in the given string ?

例如:

string line = "bla bla bla \n blabla";
bool hasEscapedSequence = (line.find("\n",0) < line.size());

这显然不会工作,因为 \\\
find 将被转义。如果我尝试(line.find(\\\\
,0)< line.size());
,似乎没有改变任何

This obviously won't work since the \n in the find will be escaped. If I try (line.find("\\n",0) < line.size());, it doesn't seem to change anything

我该怎么办?

推荐答案

line.find("\n",0)

将返回光标在其中将找到子字符串的位置。如果没有找到子字符串,它将返回string :: npos。
因此不应该与大小比较。

Will return the position of the cursor where it will find the substring. If the substring is not found it will return string::npos. And so it should not be compared with the size.

bool hasEscapedSequence = (line.find("\n") != string::npos);

这篇关于在字符串行中找到转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆