我正在尝试将字符串(忽略大小写)与向量字符串中的指定元素进行比较(我不想查看该字符串是否存在于向量中的任何位置,只有它存在于特定索引处)。
我没有像往常那样使用字符串比较成功。任何帮助将不胜感激。
答案 0 :(得分:0)
strnicmp(...)是你的工具。 'n'用于有限长度,'i'用于不区分大小写。您只需要将特定索引添加到向量:vector+pos
或&vector[pos]
int isKeyFoundAtPos( const char* key, const char* vector, int vPos) {
if( vPos > strlen(vector) ) // if you want to be safe, check for this and deal with it.
return -1;
return 0 == strnicmp( key, vector+vPos, strlen(key) );
}
...
const char* myKey = "abc";
const char* myVector = "123abc456ABC";
isKeyFoundAtPos( myKey, MyVector, 3 ); // string found
isKeyFoundAtPos( myKey, MyVector, 9 ); // string also found
我编写了包装程序作为示例,但老实说,我只是直接使用strnicmp()
。