Board index » cppbuilder » Pos and AnsiCompareIC

Pos and AnsiCompareIC


2003-12-23 05:49:12 AM
cppbuilder88
I am trying to do a case insensitive match on a string. I cant seem to figure out how to make it work with Pos() and AnsiCompareIC(), can someone offer some advice please.
AnsiString tmp = "Test String";
int MatchPos = tmp.Pos("tes");
How can you combine Pos() with AnsiCompareIC() to make it one string?
Thanks
Helena
 
 

Re:Pos and AnsiCompareIC

I am not sure if this is what you want:
bool DoesSubstringExist_I(const AnsiString S, const String Substring)
{
if (S.LowerCase().Pos(Substring.LowerCase()))
return true;
return false;
}
// Call
if (DoesSubstringExist_I("Test String", "tes" ))
ShowMessage("yes");
else
ShowMessage("no");
Rodolfo
"Helena Ramos" < XXXX@XXXXX.COM >wrote in message
Quote

I am trying to do a case insensitive match on a string. I cant seem to
figure out how to make it work with Pos() and AnsiCompareIC(), can someone
offer some advice please.
Quote

AnsiString tmp = "Test String";

int MatchPos = tmp.Pos("tes");

How can you combine Pos() with AnsiCompareIC() to make it one string?


Thanks

Helena
 

Re:Pos and AnsiCompareIC

or the Multibyte version
bool DoesSubstringExist_IM(const AnsiString S, const String Substring)
{
// case insensitive and multibyte character strings version
if (S.LowerCase().AnsiPos(Substring.LowerCase()))
return true;
return false;
}
Rodolfo
 

{smallsort}

Re:Pos and AnsiCompareIC

Or if you want the position of the substring (case insensitve)
int GetSubstringPos_I(const AnsiString S, const String Substring)
{
// case insensitive
return S.LowerCase().Pos(Substring.LowerCase());
}
int GetSubstringPos_IM(const AnsiString S, const String Substring)
{
// case insensitive and multibyte character strings version
return S.LowerCase().AnsiPos(Substring.LowerCase());
}
Rodolfo
 

Re:Pos and AnsiCompareIC

Thanks a lot for the help Rodolfo, now i know how to combine
the entire string to get the result.
Thanks again
Helena
 

Re:Pos and AnsiCompareIC

"Helena Ramos" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to do a case insensitive match on a string. I
cant seem to figure out how to make it work with Pos()
and AnsiCompareIC(), can someone offer some advice please.
Pos() is case sensitive, so you cannot use it for what you ask. One way to
search for a substring case-insensitively is to simply convert the entire
string to upper-case or lower-case first and then search it, ie:
AnsiString tmp = "Test String";
int MatchPos = tmp.LowerCase().Pos("tes");
The alternative is to just scan the original string manually (untested):
int __fastcall MyPos(const AnsiString &Str, const AnsiString &ToFind)
{
char *start = Str.c_str();
char *end = (ptr + Str.Length());
char *ptr = start;
while( ptr < end )
{
if( (end-ptr) < ToFind.Length() )
break;
if( AnsiStrLIComp(ptr, ToFind.c_str(), ToFind.Length()) == 0 )
return ((ptr-start)+1);
++ptr;
}
return 0;
}
AnsiString tmp = "Test String";
int MatchPos = MyPos(tmp, "tes");
Gambit