C++ 从字符串c+中删除单引号字符+;
我已经研究了各种方法,从string.erase/ispunct等,我不能简单地让它在我的代码中工作 我的代码如下:C++ 从字符串c+中删除单引号字符+;,c++,C++,我已经研究了各种方法,从string.erase/ispunct等,我不能简单地让它在我的代码中工作 我的代码如下: ifstream infilei("test.txt") **Second part of code......** while ( !infilei.eof() ) { string wordlist; infilei >> wordlist; inputlist.push_back(wordlist); } text.txt包含逗号、
ifstream infilei("test.txt")
**Second part of code......**
while ( !infilei.eof() )
{
string wordlist;
infilei >> wordlist;
inputlist.push_back(wordlist);
}
text.txt包含逗号、单引号、双引号等,我需要删除它们
其中infiei>>词表代码>显示,我曾尝试使用if语句删除带有“”等的字符串,但它仍然无法删除单引号或双引号。是否有其他方法或我可以将string.erase设置为高于某个ascii范围?在push_-back期间是否也可以将字符串发送到小写
谢谢你你应该像这样写if语句
if(str[i]='\''或str[i]='\''),至于小写字母,应该这样做:
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
这段代码将清除mesy_字符串中的每个“,.”:
#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;
}
#包括
#包括
#包括
使用名称空间std;
//要除去的炭
布尔有两个字符(字符c){
如果(c='\''.|c='.|c=''.'.'.|c=''.'''.''.''.|c=''.'\'''.)
返回true;
其他的
返回false;
}
int main(){
string messy_string=“dfffsg.nfgfg,nsfvvbnfn\”nsdfnsdf'ssvbssvns\”hhhfh\”;
我尝试过这个方法,但仍然没有删除信号或双引号:while(!infiei.eof()){string wordlist;while(infiei>>wordlist){for(int i=0;i>wordlist){for(int i=0;i