C++ C++;检查字符串中是否只存在以下字符
有效字符为 //ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789_/C++ C++;检查字符串中是否只存在以下字符,c++,string,iterator,compare,C++,String,Iterator,Compare,有效字符为 //ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789_/ #include <iostream> #include <string> using namespace std; int main(); { bool bIsValid = true; // test characters string strCheck("ABCDEFGHIJKLMNOPQRSTUVWXYZab
#include <iostream>
#include <string>
using namespace std;
int main();
{
bool bIsValid = true;
// test characters
string strCheck("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/");
string s("foo@?+baa") ; // should bring a "false" because of the "@?+" characters
string::const_iterator it = strCheck.begin();
// this is NOT a clever soulution has anybody a better idea ?
while (s.find(*it) != string::npos)
{
++it;
if(!s.find((*it))
{
bIsValidKey = false;
break;
}
}
cout << "Is Valid: " << bIsValid << endl ;
}
#包括
#包括
使用名称空间std;
int main();
{
bool-bIsValid=true;
//测试字符
字符串strCheck(“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789/”;
字符串s(“foo@?+baa”);//由于“@?+”字符,应带“false”
string::const_迭代器it=strCheck.begin();
//这不是一个聪明的解决方案有人有更好的主意吗?
while(s.find(*it)!=string::npos)
{
++它;
如果(!s.find((*it))
{
bIsValidKey=假;
打破
}
}
不能使用字符串::首先查找\u而不是\u of
?尝试以下方法:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
bool bIsValid = true;
string s("GHHbaa111__") ; // Add/remove other characters
regex r("^[[:alnum:]_]*$");
if (regex_match(s,r)) {
cout << "Valid string" << endl;
} else {
cout << "Invalid string" << endl;
}
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
bool-bIsValid=true;
字符串s(“GHHbaa111_u”);//添加/删除其他字符
正则表达式r(“^[:alnum:][uuz]*$”;
if(正则表达式匹配(s,r)){
不能包含
#包括
int main(){
bool-bIsValid=true;
//测试字符
std::string strCheck(“abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzo123456789/”;
std::string s(“foo@?+baa”);//由于“@?+”字符,应带“false”
如果(s.find_first_not_of(strCheck)!=std::string::npos)
bIsValid=false;
std::能否请修复if(!s.find((*it))
带括号的if(!s.find((*it))的问题在哪里?我知道一定有(*it)。somthing()但是什么?我建议你看看正则表达式库:或者谷歌C++ RXEXP。它可能需要一些学习,但是一旦掌握它,就可以节省很多时间。这是个好主意,我会看到。我用ReGEX发布了一个例子。它非常灵活,不必键入所有有效字符。
#include <iostream>
#include <string>
int main() {
bool bIsValid = true;
// test characters
std::string strCheck("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/");
std::string s("foo@?+baa") ; // should bring a "false" because of the "@?+" characters
if (s.find_first_not_of(strCheck) != std::string::npos)
bIsValid = false;
std::cout << "Is Valid: " << bIsValid << std::endl ;
}