Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching an AnsiString in Borland C++ Builder 6 Trial 1

Status
Not open for further replies.

Ruggie

Programmer
Aug 2, 2003
46
US
I'm trying to write a program that looks for diffrent symbols such as ' or " and halts a loop.
In the loop all it does is take existing text from a memo and add stuff in front and behind.

My problem is I can't figure out how to search for one symbol in a single string.
I know you can use IndexOf to search a list of strings but that doesn't help me.

Any insight on the subject is welcome.

Thanks,
Ruggie
~
Heres a copy of the code I'm working with so you get a better idea of what I'm doing. The goal is to take html and strip it or transform it so it can be redrawn with javascript. But javascript is sensitive and the html code needs to be checked for sybols and action needs to be taken before the loop copies over the information.

Theres 3 TMemo fields.
Memo1 is the one the user types in.
CacheMemo is the one we copy the information in.
Memo2 is in a diffrent window and CacheMemo just gets copied over.
(I will most likely take this down to just two but for now its easier for me this way)
~
CacheMemo->Clear();
int TotalLines = Memo1->Lines->Count;
int CurrentLine = 0;

while (CurrentLine <= TotalLines && CurrentLine != TotalLines){
CacheMemo->Lines->Add(&quot;document.write('&quot; + Memo1->Lines->Strings[ignore][CurrentLine][/ignore] + &quot;');&quot;);
CurrentLine++;
}
~
Once again...

Thanks!!!
 
I'm still somewhat new to c++ so can you please suggest a way to use that?
Remember, I need to scan the string searching for a ' symbol.
Then I need to modify that symbol.
And then I have to keep scanning to make sure there isn't two ' markings on the same line
(In JavaScript and HTML there usually is)

Thanks in advanced!
 
I'm doing this from memory so this may have some errors in it. User beware!
Code:
AnsiString MyString;
// MyString get data from somewhere
int StrLen = MyString.Length();
for (int j = 1; j <= StrLen; j++)
{
    // Note that AnsiStrings start at 1 not 0
    if (MyString[j] == &quot;\'&quot;) // note the back slash
    {
        MyString[j] = &quot;\&quot;&quot;;
     }
}

There are other ways to do this, too.

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
I thought when you used the [ignore][][/ignore] operator on a string it acted as a multiline string. 0 being the first line and so on.
I'm working with a memo box and I access the lines using that method. Is it possible to access each letter that way also?
~
I'm using:
[ignore]Memo1->Lines->Strings[CurrentLine][/ignore]
To pull 1 line out of the memo box, where CurrentLine is the current line in the loop to remove/work with from the box.
~
I like your method it would be easy to enter in a loop.
And would allow me to check each letter.
Any suggestions are welcome. Thnx.
~~~~~~~~~~~~~
P.S. - I tried StrPos and it worked for catching and allowing me to add/do something.
BUT, I don't know how to find the location of that symbol and allow me to replace/remove it.

Thanks in advance! :)
 
Try:
Code:
MyString = Memo1->Lines->Strings[CurrentLine];
.
.
.
There a difference between Strings and AnsiString (or String).

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Well I tried it out like you suggested but now I'm getting errors and I'm not quite sure how to handle them.
Heres the current code:
[ignore]
int TotalLines = Memo1->Lines->Count;
int CurrentLine = 0;
AnsiString MyString;
int StrLen;

while (CurrentLine <= TotalLines && CurrentLine != TotalLines){
MyString = Memo1->Lines->Strings[CurrentLine];
StrLen = MyString.Length();
for (int j = 1; j <= StrLen; j++)
{
if (MyString[j] == &quot;\'&quot;)
{
MyString[j] = &quot;\&quot;&quot;;
}
}
CacheMemo->Lines->Add(&quot;document.write('&quot; + MyString + &quot;');&quot;);
CurrentLine++;
}
[/ignore]

When I try to compile the result I get these errors:

E2034 Cannot convert 'char' to 'char *'
E2034 Cannot convert 'char *' to 'char'

In that order. When I seek to the part of the code that is causing the error I find:
For the first one it is if (MyString[j] == &quot;\'&quot;)
For the second one it is MyString[j] = &quot;\&quot;&quot;;

I'm pretty sure I copied your example and have it entered correctly.

Suggestions please?

Thanks
~
P.S. - If Strings start at 1 and not 0 then wouldn't that loop start the process at the 2nd letter and not the first?
Just a thought.
 
I was able to get around the errors by changing the code a bit. I'm not at the computer that has the code on it but I'm pretty sure I ran something like this:
[ignore]
int TotalLines = Memo1->Lines->Count;
int CurrentLine = 0;
AnsiString MyString;
AnsiString MyString2;
int StrLen;

while (CurrentLine <= TotalLines && CurrentLine != TotalLines){
MyString = Memo1->Lines->Strings[CurrentLine];
StrLen = MyString.Length();
for (int j = 1; j <= StrLen; j++)
{
MyString2 = MyString[j];
if (MyString2 == &quot;\'&quot;)
{
MyString2 = &quot;\&quot;&quot;;
}
MyString[j] = MyString2;
}
CacheMemo->Lines->Add(&quot;document.write('&quot; + MyString + &quot;');&quot;);
CurrentLine++;
}
[/ignore]


Now my problem is that I forgot that I need to replace 1 letter with 2 letters.
And this method only allows you to change the single letter you are dealing with, even if I try to pass MyString2 something like test it will only see t when I return it to MyString[j]

Example, 'help' needs to become \'help\'
(and the same thing for &quot; marks)

To approach this I changed the code to look like this:
[ignore]
int TotalLines = Memo1->Lines->Count;
int CurrentLine = 0;
AnsiString MyString;
AnsiString MyString2;
int StrLen;

while (CurrentLine <= TotalLines && CurrentLine != TotalLines){
MyString = Memo1->Lines->Strings[CurrentLine];
StrLen = MyString.Length();
for (int j = 1; j <= StrLen; j++)
{
MyString2 = MyString[j];
[/ignore]
if (MyString2 == &quot;\'&quot;)
{
MyString.replace(j,1,&quot;\\\'&quot;);
StrLen++;j++;
}
[ignore]
}
CacheMemo->Lines->Add(&quot;document.write('&quot; + MyString + &quot;');&quot;);
CurrentLine++;
}
[/ignore]

~
In the Builder help file it shows .replace as a string modifier, when I try to use it I get the message:
replace is not a member of AnsiString

I also tried:
AnsiStringReplace(MyString,j,&quot;\\\'&quot;)
But it demands flags be passed also:
AnsiStringReplace(MyString,j,&quot;\\\'&quot;,Flags)
But I can't find information on how to transfer any flags to the command.

I'd like to just use .replace like it shows in the help file but I don't know why it doesn't work. Do I have to include a header file?

Let me know what you think.

Thanks,
Keith
 
I figured it out and got it working!
I think code should be shared to everyone, so heres the finished and working product:
~[ignore]
Main->Enabled = false;
CacheMemo->Clear(); [/ignore]
[ignore]//Should be empty[/ignore][ignore]
AnsiString MyString;
AnsiString MyString2;
int TotalLines = Memo1->Lines->Count;
int CurrentLine = 0;
int StrLen;
while (CurrentLine <= TotalLines && CurrentLine != TotalLines){
MyString = Memo1->Lines->Strings[CurrentLine].c_str();
StrLen = MyString.Length();
for (int j = 1; j <= StrLen; j++)
{
MyString2 = MyString[j];
if (MyString2 == &quot;\'&quot;)
{
MyString.Insert(&quot;\\&quot;,j);
StrLen++; [/ignore]
[ignore]// Keep the numbers right. [/ignore][ignore]
j++;[/ignore]
[ignore] // Same ^[/ignore][ignore]
}
}
CacheMemo->Lines->Add(&quot;document.write('&quot; + MyString + &quot;');&quot;);
CurrentLine++;
}

Main->WindowState = wsMinimized;
Results = new TResults(this);
Results->Memo2->Lines = CacheMemo->Lines;
Results->Memo2->Modified = false;
[/ignore]

So when you enter:
Thnx '2ffat' for the info.
You get the output:
document.write('Thnx \'2ffat\' for the info.');

Of couse it doesn't end here, mode code will go into it to catch mode situations, but the hard part is over.

Thanks for all the ideas,
Keith
(ruggie1of1@hotmail.com)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top