急问::string问题。

laoer1314 2008-03-24 03:03:51
string能存储\n\r等非打印字符吗?如果能存储的话在string里是什么呢?貌似好像是0.
如果能存储的话我要把string里的这些非打印字符都删掉?请问怎么实现呢
...全文
77 5 打赏 收藏 转发到动态 举报
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Supper_Jerry 2008-03-24
  • 打赏
  • 举报
回复
修改了一下,需要删除的字符串拿出来作为全局变量
#include <iostream>
#include <string>
using namespace std;
string strDel = "\r\n";//需要删除的字符
void DelRtn(string& str)
{
int nPos = 0;
while((nPos = str.find_first_of(strDel, 0))!= string::npos)
{
str.erase(nPos, 1);
}
}
int main()
{
string str("hello\r\n,world!");
cout << str << endl;
DelRtn(str);
cout << str << endl;
return 0;
}
Supper_Jerry 2008-03-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
void DelRtn(string& str)
{
string strDel = "\r\n";
int nPos = 0;
while((nPos = str.find_first_of(strDel, 0))!= string::npos)
{
str.erase(nPos, 1);
}
}
int main()
{
string str("hello\r\n,world!");
cout << str << endl;
DelRtn(str);
cout << str << endl;
return 0;
}
jieao111 2008-03-24
  • 打赏
  • 举报
回复
6.9 处理标点符号
我们已经把每个文本行分解成独立的单词现在需要把单词中可能附加的标点符号去掉
例如下列文本行
magical but untamed. "Daddy, shush, there is no such thing,"
被分解成
magical
but
untamed.
"Daddy,
shush,
there
is
no

such
thing,"
怎样去掉不想要的标点呢首先我们将定义一个字符串它包含我们希望去掉的所有
标点
string filt_elems( "\",.;:!?)(\\/" );
\"和\\序列表示第一个序列中的引号和第二个序列中的第二个反斜杠被视为该字符串
中的文字元素而不是字符串的结尾或下一行的续行符号
接下来我们将用find_first_of()操作在我们的字符串里找到每个匹配元素
while (( pos = word.find_first_of( filt_elems, pos ))
!= string::npos )
最后我们需要用erase()去掉字符串中的标点
word.erase(pos, 1);
这个版本的erase()操作的第一个参数表示字符串中要被删除的字符的开始位置第二个
参数是可选的表示要被删除的字符的个数在我们的例子中我们正在删除位置pos 上的
字符如果我们省略第二个参数则erase()将删除从pos 到字符串结束的所有字符
下面是filter_text()的完整实现它有两个参数指向包含文本的string vector 的指针
以及含有要过滤的元素的string 对象
void
filter_text( vector<string> *words, string filter )
{
vector<string>::iterator iter = words ->begin();
vector<string>::iterator iter_end = words ->end();
// 如果用户没有提供filter, 则缺省使用最小集
if ( ! filter.size() )
filter.insert( 0, "\".," );
while ( iter != iter_end ) {
string::size_type pos = 0;
// 对于找到的每个元素, 将其删除
while (( pos = (*iter).find_first_of( filter, pos ))
!= string::npos )
(*iter).erase(pos,1);
iter++;
}
}
你知道为什么不随循环的每一次迭代递增pos 吗也就是下列代码为什么是不正确的

while (( pos = (*iter).find_first_of( filter, pos ))
!= string::npos )
{
(*iter).erase(pos,1);
++pos; // 下正确 ...
}
238 第六章 抽象容器类型
pos 表示string 中的位置例如已知字符串
thing,"
循环的第一次迭代向pos 赋值5 即逗号的位置在去掉逗号之后字符串变成
thing"
位置5 现在是双引号如果我们已经递增pos 那么将不能找到并去掉这个标点符号
下面给出怎样在主程序中调用filter_text()
string filt_elems( "\",.;:!?)(\\/" );
filter_text( text_locations ->first, filt_elems );
sheenl 2008-03-24
  • 打赏
  • 举报
回复
erase(remove_if(str.begin(), str.end(), ...), str.end());
独孤过儿 2008-03-24
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str("hello\r\n,world!");
cout << str << endl;
return 0;
}


如果想删掉,那就从头往后遍历string,找到了就删除。或者直接调用string类的成员函数...
相关推荐

63,594

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下