从SQLCHAR对象中解析出非Alpha数字字符

时间:2009-09-12 22:05:36

标签: c++ sql

我目前有一堆来自数据库查询的SQLCHAR对象。查询结果存储在std :: string中,然后绑定到各个SQLCHAR变量。需要解析其中一些变量以删除任何非字母数字字符。这里最好的方法是什么?

我已经实现了对std :: string ...

的基本解析
for (std::string::iterator i = str.end()-1; i >= str.begin(); --i)
{
    if ( !isalpha(*i) && !isdigit(*i) ) 
    {
        str1.erase(i);
    } 
}

但是现在我遇到了将SQLCHAR转换为std :: string然后再返回的问题。有一个更好的方法吗?

2 个答案:

答案 0 :(得分:1)

考虑这个伪代码

bool is_not_alnum(char c){return !isalnum(c);}
unsigned char* s = ()blah_as_sql_char; //somehow its gotta cast to cstr right?
std::remove_if(s, strlen(s), is_not_alnum);
SQLCHAR result = (SQLCHAR)s; //cast it back however

http://www.cplusplus.com/reference/clibrary/cctype/isalnum/
http://www.sgi.com/tech/stl/remove_if.html

答案 1 :(得分:0)

您是否在寻找易于维护或更好的性能?

boost regex可以帮助维护

对于性能,我会查看标准STL ... std :: remove_if算法

相关问题