Board index » cppbuilder » Search string

Search string


2007-01-24 11:53:17 PM
cppbuilder62
Hello all
I need to find the position of the substring in a string
The AnsiPos() is doing all what i want.
But I have a text like
<html>
<item>
Hello
</item>
<item>
Welcom
</item>
</html>
How to get the position of the second occurence of the string "<item>"
Thanks for the help
SA
 
 

Re:Search string

bar wrote:
Quote
How to get the position of the second occurence of the string
"<item>"
something like this (untested)
int pos1 = AnsiPos(TheString,TheSubstring);
if (pos1>0)
{
AnsiString Test = TheString.SubString(pos1,
TheString.Length()-pos1);
int pos2 = AnsiPos(Test,TheSubstring);
}
--
liz
 

Re:Search string

Hello Liz
thanks for the reply
Since i have many occurences of the string "<item>"
Is there is any alternate function to start searching from the specified
position in a string.
Thanks
SA
"Liz" < XXXX@XXXXX.COM >wrote in message
Quote
bar wrote:

>How to get the position of the second occurence of the string
>"<item>"

something like this (untested)

int pos1 = AnsiPos(TheString,TheSubstring);

if (pos1>0)
{
AnsiString Test = TheString.SubString(pos1,
TheString.Length()-pos1);
int pos2 = AnsiPos(Test,TheSubstring);
}

--
liz
 

{smallsort}

Re:Search string

bar wrote:
Quote
Hello Liz
thanks for the reply

Since i have many occurences of the string "<item>"
Is there is any alternate function to start searching from the
specified position in a string.
One way would be to set the CommaText property of a TStringList to
/your/ string, and then replace the <item>and </item>tags with ","
(or set the DelimitedText property, and then the delimiter). Check
StringReplace() for how. The end result would be a TStringList, each
element of which was one of your strings.
Another way is to use AnsiPos() in a loop. Something like this:
AnsiString CheckIt = MyString;
int Pos1;
while ( (Pos1 = AnsiPos(CheckIt, TheSubString))>0)
{
int Len = CheckIt.Length() - Pos1 - TheSubString.Length();
AnsiString Temp = CheckIt.Substring(1,Pos1); // part before substr
CheckIt = CheckIt.SubString(Pos1+TheSubString.Length(), Len);
}
--
liz