| View previous topic :: View next topic |
| Author |
Message |
Rodolfo Frino - Macrosoft Guest
|
Posted: Tue Dec 23, 2003 2:51 am Post subject: 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" <hcramos (AT) swbell (DOT) net> wrote
| 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
|
|
|
| Back to top |
|
 |
Rodolfo Frino - Macrosoft Guest
|
Posted: Tue Dec 23, 2003 3:03 am Post subject: 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
|
|
| Back to top |
|
 |
Rodolfo Frino - Macrosoft Guest
|
Posted: Tue Dec 23, 2003 5:09 am Post subject: 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
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 23, 2003 7:29 pm Post subject: Re: Pos and AnsiCompareIC |
|
|
"Helena Ramos" <hcramos (AT) swbell (DOT) net> wrote
| 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
|
|
| Back to top |
|
 |
|