2012-03-21 226 views
0

我看了各种方法,从string.erase/ispunct等等,我不能简单地得到它为我的代码工作。从字符串c中删除单引号字符C++

我的代码如下:

ifstream infilei("test.txt") 

**Second part of code......** 

while (!infilei.eof()) 
{ 
    string wordlist; 
    infilei >> wordlist; 
    inputlist.push_back(wordlist); 
} 

的的text.txt包含逗号,单引号,双引号等等,我需要将它们删除。

其中infilei >> wordlist;显示,我试图使用if语句删除字符串与'“'等,但它仍然不会删除单引号或双引号。是否有另一种方法或我可以设置一个string.erase为?超过一定ASCII范围,是一种方法,也送字符串的push_back期间小写

谢谢

+0

向我们展示您尝试的“if语句”。 – 2012-03-21 21:21:03

+0

我试过这个,但仍然没有删除信号或双引号: while(!infilei.eof()){string wordlist; while(infilei >> wordlist){for(int i = 0; i MacKey 2012-03-21 21:54:39

回答

1

你应该写if语句,像这样if(str[i]=='\"' or str[i]=='\'')并以此为小写这应该这样做?

std::transform(str.begin(), str.end(), str.begin(), ::tolower); 
+0

我已经试过了,并且会重新试一试,谢谢 – MacKey 2012-03-21 20:09:50

+0

我试过了,但仍然没有删除信号或双引号: \t while(!infilei.eof )) \t { \t \t串单词表; \t \t而(infilei >>单词表) \t \t { \t \t \t for(int i = 0;我 MacKey 2012-03-21 20:13:28

+0

std :: transform(str.begin(),str.end(),str.begin(),:: tolower);完美的作品。 – MacKey 2012-03-21 22:36:01

1

这段代码将清理每个“,”。 'from mesy_string:

#include <iostream> 
#include <algorithm> 
#include <string> 

using namespace std; 

//Chars to be removed 
bool has_chars(char c){ 
    if(c=='\"' || c=='.' || c==',' || c=='\'') 
    return true; 
    else 
    return false; 
} 

int main() { 
    string messy_string="dfffsg.nfgfg,nsfvfvbnf\"nsdfnsdf\'ssvbssvns\"hhhfh\""; 

    cout<< messy_string<<endl; 

    remove_if (messy_string.begin(), messy_string.end(), has_chars); 

    cout<< messy_string<<endl; 
    return 0; 
} 

您应该可以根据需要进行修改。

+0

尝试,但无济于事。感谢您的帮助。 – MacKey 2012-03-21 21:56:34