 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ChoyKw Guest
|
Posted: Tue May 17, 2005 8:28 am Post subject: Delimiter in TStringList |
|
|
Sorry to repost this, I misposted the original post to anothe thread.
Hi all,
I tried to use a TStringList to seperate a sentance, but I want to use a
specific character, instead of the preset space, comma or other. Since I'm
seperating normal sentaces, theses characters are commonly used in the
sentaces, instead, I'd like to use like '@' as a delimiter character. I tried
setting '@' as the Delimiter in the TStringList and load the sentaces with
DelimitedText. Still, the TStringList insisted to seperate my texts using
spaces, commas and semicolons. Is there a way to tell TStringList to ignore
space, comma and semicolon (actually, semicolon is okay, since it is not likely
I'll have it in any of the target text I'm processing, so I really need the
TStringList to ignore space and comma only.)
i.e:(please view with fixed width font)
The phrase "I am not going; to do that!" and I'd like it to be seperated into:
^
delimiter
"I am not going"
" to do that!"
instead of
"I"
"am"
"not"
"going"
"to"
"do"
"that!"
(for anyone ever curious why I'm doing that, the answer is due to screen space
constrain)
Thanks In Advance.
Regards.
--
-------------------------------------------------------------
"Aaaaah yourself!.....Uh, oh-o!"
-Serious 'Second Encounter' Sam-
-------------------------------------------------------------
|
|
| Back to top |
|
 |
Vladimir Stefanovic Guest
|
Posted: Tue May 17, 2005 9:38 am Post subject: Re: Delimiter in TStringList |
|
|
I'm not sure how to change the delimiting rules (probably is
possible), but you can easily make your own code for that.
The code can probably be more compact but this is one quick
version I made...
// This function should partition your ansi string to pieces
// which should be separated by any character you choose.
void TForm1::ParseAnsiStringToStrings( AnsiString AString, char AChar, TStrings *AStrings )
{
if ( AString.Length() == 0 )
return;
AString = AString + AChar;
AStrings->Clear();
char *niz = new char[ AString.Length() + 1 ];
char *pom = new char[ AString.Length() + 1 ];
strcpy( niz, AString.c_str() );
int prev = 0;
for ( int i=0; i<AString.Length(); i++ )
{
if ( ( niz[i] == AChar ) || ( i == AString.Length()-1 ) )
{
strncpy( pom, &niz[prev], i-prev );
pom[i-prev] = ' ';
AStrings->Add( AnsiString( pom ) );
prev = i + 1;
}
}
delete [] pom;
delete [] niz;
}
// This function should concatenate items from strings
// to ansi string separated by any character you choose.
AnsiString TForm1::StringsToAnsiString( TStrings *AStrings, char AChar )
{
AnsiString Delimited;
for ( int i=1; i<=AStrings->Count; i++ )
Delimited += AStrings->Strings[i-1] + AChar;
Delimited.SetLength( Delimited.Length()-1 );
return( Delimited );
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ParseAnsiStringToStrings( Edit1->Text, ';', ListBox1->Items );
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Edit2->Text = StringsToAnsiString( ListBox1->Items, ';' );
}
Best regards,
Vladimir Stefanovic
|
|
| Back to top |
|
 |
JD Guest
|
|
| Back to top |
|
 |
ChoyKw Guest
|
Posted: Tue May 17, 2005 10:43 am Post subject: Re: Delimiter in TStringList |
|
|
Vladimir Stefanovic wrote:
| Quote: |
I'm not sure how to change the delimiting rules (probably is
possible), but you can easily make your own code for that.
The code can probably be more compact but this is one quick
version I made...
|
<code snipped>
| Quote: | Best regards,
Vladimir Stefanovic
|
Hi,
Thanks for your reply. Your answer has inspired me to do the following:
//---------------------------------------------------------------------
int strindex;
TStringList *strl = new TStringList;
AnsiString str = quests->Strings[qindex];
//quests is another TStringlist containing the whole question database
//The whole thing is for printing some questions on screen
strl->Clear();
strindex = str.Pos(";");
if(strindex == 0)
strl->Add(str);
else
{
do
{
strl->Add(str.SubString(0, strindex - 1));
str = str.SubString(strindex + 1, str.Length()-strindex);
}while((strindex = str.Pos(";")) != 0);
strl->Add(str);
}
//---------------------------------------------------------------------
but I'm still not very happy about this, since the loop made the process pretty
slow. My app should be able to run well on a minimum P2 300 (I use a Duron
900MHz for development - and this approach is noticeably slow even on this
machine). Also, it's a DirectDraw App, and there is a processing loop (in other
words, this app is sort of like a game) - though it made use of GDI to draw the
text on screen, so a do-while loop is pretty expensive in this app. Anyone have
any suggestion? I tried with the original idea of using delimated text, though
it made the display a mess by spliting at the wrong location, it is still much
faster than my current approach.
TIA.
Regards.
--
-------------------------------------------------------------
"Aaaaah yourself!.....Uh, oh-o!"
-Serious 'Second Encounter' Sam-
-------------------------------------------------------------
|
|
| Back to top |
|
 |
ChoyKw Guest
|
Posted: Tue May 17, 2005 11:10 am Post subject: Re: Delimiter in TStringList |
|
|
JD wrote:
| Quote: |
You don't need to do that. First, I don't understand what
you're doing to encounter a problem. Each entry is just an
AnsiString.
|
I was originally asking for help on how to use the TStringList->Delimiter, and
how to force any character, such as '@' or '#' to be used as the one and only
one delimiter to seperate the DelimitedText, so that I can insert a '@' in the
text to make the TStringList seperate the text at the location where I inserted
the '@' and ignore the default delimiter such as space and comma and tab and
semicolon, but later I decide that semicolon is acceptable, since none of the
texts I need to process had any semicolon in them. So finally, my question
became: How do I force the TStringlist to parse my text using ';' and only ';'
to break the text, while ignoring other default delimiter like space and comma
and tab.
| Quote: | I'm guessing that you're using LoadFromFile.
|
Yes, the original text is loaded from file.
| Quote: | All you have to do is use AnsiQuotedStr to format your strings
|
I haven't tried AnsiQuotedStr...will check it tomorrow when I return to work.
| Quote: | so that you can use any charaters and the strings can be
easily parsed using CommaText:
|
Isn't CommaText using only comma as Delimiter? That will not be acceptable,
since one of the subject that I'll be working on is Maths and there will be
numbers using comma as unit markers like:
2,356,704
I don't want it to be broken down to
2
356
704
See?
| Quote: |
If you need a better sample, you need to be more specific
with what you're doing.
|
I thought I'm already pretty specific. Anyway, the sample isn't what I'm really
looking for, since I'm not going to use CommaText. Sorry about that. I've got a
partial solution in another reply, but is still looking for a way to make the
TString/TStringList to parse the text using one (and only one) specific char as
delimiter.
Regards.
--
-------------------------------------------------------------
"Aaaaah yourself!.....Uh, oh-o!"
-Serious 'Second Encounter' Sam-
-------------------------------------------------------------
|
|
| Back to top |
|
 |
Vladimir Stefanovic Guest
|
Posted: Tue May 17, 2005 1:08 pm Post subject: Re: Delimiter in TStringList |
|
|
If the speed is the problem, you might consider using
TMemoryStream for direct manipulation with its stored
data.
--
Best regards,
Vladimir Stefanovic
"ChoyKw" <newsreader (AT) newsgroup (DOT) com> wrote
| Quote: | Vladimir Stefanovic wrote:
I'm not sure how to change the delimiting rules (probably is
possible), but you can easily make your own code for that.
The code can probably be more compact but this is one quick
version I made...
code snipped
Best regards,
Vladimir Stefanovic
Hi,
Thanks for your reply. Your answer has inspired me to do the following:
//---------------------------------------------------------------------
int strindex;
TStringList *strl = new TStringList;
AnsiString str = quests->Strings[qindex];
//quests is another TStringlist containing the whole question database
//The whole thing is for printing some questions on screen
strl->Clear();
strindex = str.Pos(";");
if(strindex == 0)
strl->Add(str);
else
{
do
{
strl->Add(str.SubString(0, strindex - 1));
str = str.SubString(strindex + 1, str.Length()-strindex);
}while((strindex = str.Pos(";")) != 0);
strl->Add(str);
}
//---------------------------------------------------------------------
but I'm still not very happy about this, since the loop made the process
pretty
slow. My app should be able to run well on a minimum P2 300 (I use a Duron
900MHz for development - and this approach is noticeably slow even on this
machine). Also, it's a DirectDraw App, and there is a processing loop (in
other
words, this app is sort of like a game) - though it made use of GDI to
draw the
text on screen, so a do-while loop is pretty expensive in this app. Anyone
have
any suggestion? I tried with the original idea of using delimated text,
though
it made the display a mess by spliting at the wrong location, it is still
much
faster than my current approach.
TIA.
Regards.
--
-------------------------------------------------------------
"Aaaaah yourself!.....Uh, oh-o!"
-Serious 'Second Encounter' Sam-
-------------------------------------------------------------
|
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue May 17, 2005 2:13 pm Post subject: Re: Delimiter in TStringList |
|
|
"ChoyKw" <newsreader (AT) newsgroup (DOT) com> wrote:
| Quote: |
[...] Isn't CommaText using only comma as Delimiter?
|
No. I explicitly stated that as long as you format the string
correctly, you can include *any* charater in the string and
still have it parse the way that you want.
| Quote: | [...] there will be numbers using comma as unit markers like:
2,356,704
I don't want it to be broken down to
2
356
704
See?
|
I understand but you do not. If you format your string:
"2,356,704"
it will not be broken up by the comma's.
| Quote: | I thought I'm already pretty specific. [...]
|
You failed to say if you were loading from a file or if you
were hard-coding the text and adding it into the StringList.
That would make a difference as to what would be a better
solution.
| Quote: | Anyway, the sample isn't what I'm really looking for, since
I'm not going to use CommaText.
|
Your loss because it is exactly what you're looking for. Too
bad you didn't look close enough. The solution is in how you
format the string. Do it correctly and all you need do is call
CommaText.
| Quote: | I've got a partial solution in another reply,
|
I saw that but it certainly is not an improvement over one
line of code.
| Quote: | but is still looking for a way to make the TStringList to
parse the text using one (and only one) specific char as
delimiter.
|
That is the wrong approach. Simply enclose the string with
double quotes (and if you need a quote in the string, escape
it - which is what AnsiQuotedStr does for you).
Assuming that you have used LoadFromFile and formated a single
string to break:
"First Part","Second Part"
then all you need to do is to use a second StringList and
CommaText (as the example shows) and the resulting StringList
will be:
First Part
Second Part
and that's EXACTLY what you were try to accomplish.
~ JD
|
|
| Back to top |
|
 |
ChoyKw Guest
|
Posted: Wed May 18, 2005 2:14 am Post subject: Re: Delimiter in TStringList |
|
|
JD wrote:
<snipped>
| Quote: | Assuming that you have used LoadFromFile and formated a single
string to break:
"First Part","Second Part"
then all you need to do is to use a second StringList and
CommaText (as the example shows) and the resulting StringList
will be:
First Part
Second Part
and that's EXACTLY what you were try to accomplish.
~ JD
|
hmm...I get what you mean. The idea the designer of DelimatedText had is
totally different from what I had in mind. I'll try what you suggested - that
means I'll have to edit the whole database (groan...).
--
-------------------------------------------------------------
"Aaaaah yourself!.....Uh, oh-o!"
-Serious 'Second Encounter' Sam-
-------------------------------------------------------------
|
|
| Back to top |
|
 |
Thorsten Kettner Guest
|
Posted: Wed May 18, 2005 12:46 pm Post subject: Re: Delimiter in TStringList |
|
|
Hi ChoyKw,
CommaText and DelimitedText work not as one would expect, but
separate also at blank positions. Look for ExtractStrings
instead. This function splits a string into a TStringList
using only the specified separators.
"ChoyKw" <newsreader (AT) newsgroup (DOT) com> wrote:
[snip]
| Quote: | I tried to use a TStringList to seperate a sentance, but I
want to use a specific character, instead of the preset
space, comma or other. Since I'm seperating normal sentaces,
theses characters are commonly used in the sentaces, instead,
I'd like to use like '@' as a delimiter character.
[snip] |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|