Mega Search
23.2 Million


Sign Up

Make a donation  
TStringList - No blanks and no spaces allowed?  
News Group: embarcadero.public.delphi.language.delphi.general

Howdi gang

I'm using a TStringList in one of my servers for passing data around. It's 
ideal for me to use simply because of the values property which allows my 
program to set and find data / objects with ease.

Anyway turns out the little critter doesn't allow you to set it's string 
with spaces and it doesn't like blanks either.

Now whats wrong with me doing this?:

var
SList: TStringList;
begin
SList.Values['Test'] := ''; //Doesn't work
SList.Values['This'] := 'out here';  //Also doesn't work
end;

Now why would anyone impose such a stupid limitation that will screw that 
up? If I want to set a string with spaces, why is this a problem? The help 
file says that this format is the same as a windows ini file.. and even 
those have a carriage return at the end of each line!

So.. how does one get around this problem? I'm using this in a TCP client / 
server in order to form data packets and them sending the SList.Text 
property so that it all goes down the wire at once. The snag is that with 
the above fault, i can't send blank datafields or anything with spaces 
unless i base64 encode it (which doesn't work well for blank strings). Thats 
not such a big problem but then it means i need to do it manually for every 
lot of data sent or received! I did mull over creating a descendant class 
but it appears that the limitation is so deep rooted that i'd never be able 
to dig it out / override it.

Is there a 3rd party equivalent or hack that does away with this pain of a 
limitation?

JC

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 5:07 PM EST
From: Justin Case
 
Re: TStringList - No blanks and no spaces allowed? [Edit]  
News Group: embarcadero.public.delphi.language.delphi.general
Justin wrote:

>> SList.Values['Test'] := ''; //Doesn't work - Yes it deletes the key 'Test'
>> 
> Which is annoying for me.

That behavior is by design.

If you want to store a blank string and still be able to access it by Name, 
you can do this instead:

{code}
I := SList.IndexOfName('Test');
if I = -1 then
  SList.Add('Test=')
else
  SList.Strings[I] := 'Test=';
{code}

>> SList.Values['This'] := 'out here';  //Also doesn't work - Yes it does
>> 
> Not in D6 it doesn't! Believe me.. i spent hours chasing my tail round
> on this.

I use BCB 6 (which uses the same VCL/RTL as Delphi 6) every day at my day 
job.  Trust me, the Names[] and Values[] properties happily accept strings 
with spaces in them.  It is only when assigning a blank string to the Values[] 
property that you run into a problem (and only if you want to keep the Name 
in the list).

> From the help file:

Read it again more carefully.  When you use the Values[] property, it works 
with strings that are stored in 'Name=Value' format in the list.  The portion 
of the documentation that you quoted is referring to the **entire 'Name=Value' 
string as a whole**, not the Name or Value sub-portions.

For instance:

{code}
SList.Values['This '] := 'that'; // string stored is 'This =that'
SList.Values['This'] := ' that'; // string stored is 'This= that'
SList.Values['This '] := ' that'; // string stored is 'This = that'
{code}

That being said, the documentation says **should**.  The above actually works 
as shown.  The Names[] and Values[] properties look for just the '=' by itself, 
treating everything before it (spaces and all) as a Name, and everything 
after it (spaces and all) as a Value.  There is no trimming involved.  Look 
at the RTL's source code for TStrings in Classes.pas and you will see this 
to be true.

> No spaces before or after the equals sign - and after is the problem I
> am having.

No, it is not.  You are having a problem with blank strings, not strings 
with spaces in them.

> I suppose i could replace spaces with [space] but then i'd need to
> do that / check that for everything. There must be a better way?

No, you do not need to resort to that extreme.

> This annoying 'feature' must have been changed in later versions
> of delphi.

Nope.  It has always worked this way, in every version.  The only change 
that has been made is the introduction of the NameValueSeparator and ValueFromIndex 
properties in Delphi 7.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 6:53 PM EST
From: Remy Lebeau (TeamB)
 
Re: TStringList - No blanks and no spaces allowed? [Edit]  
News Group: embarcadero.public.delphi.language.delphi.general
 wrote in message news:709686@forums.embarcadero.com...
>  SList: TStringList.create; //You really need this bit

Yes in my hasty rush to make this thread i didn't bother including that (but 
rest assured it is in my code)

>  SList.Values['Test'] := ''; //Doesn't work - Yes it deletes the key 
> 'Test'

Which is annoying for me.

>  SList.Values['This'] := 'out here';  //Also doesn't work - Yes it does

Not in D6 it doesn't! Believe me.. i spent hours chasing my tail round on 
this.

From the help file:

property Values[const Name: string]: string;

The Name that identifies the string is to the left of the equal sign (=), 
and the current Value of the Name identifier is on the right side. There 
should be no spaces present before or after the equal sign.

No spaces before or after the equals sign - and after is the problem I am 
having. I suppose i could replace spaces with [space] but then i'd need to 
do that / check that for everything. There must be a better way?

> Space in the string works fine for me !!!

This annoying 'feature' must have been changed in later versions of delphi.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 6:14 PM EST
From: Justin Case
 
Re: TStringList - No blanks and no spaces allowed? [Edit]  
News Group: embarcadero.public.delphi.language.delphi.general
> {quote:title=Justin Case wrote:}{quote}
> Howdi gang
> 
> I'm using a TStringList in one of my servers for passing data around. It's 
> ideal for me to use simply because of the values property which allows my 
> program to set and find data / objects with ease.
> 
> Anyway turns out the little critter doesn't allow you to set it's string 
> with spaces and it doesn't like blanks either.
> 
> Now whats wrong with me doing this?:
> 
> var
> SList: TStringList;
> begin
> SList.Values['Test'] := ''; //Doesn't work
> SList.Values['This'] := 'out here';  //Also doesn't work
> end;
> 
> Now why would anyone impose such a stupid limitation that will screw that 
> up? If I want to set a string with spaces, why is this a problem? The help 
> file says that this format is the same as a windows ini file.. and even 
> those have a carriage return at the end of each line!
> 
> 
> JC

{code}
var
  SList: TStringList;
begin
  SList: TStringList.create; //You really need this bit
  SList.Values['Test'] := ''; //Doesn't work - Yes it deletes the key 'Test'
  SList.Values['This'] := 'out here';  //Also doesn't work - Yes it does
end;
{code}

'' deletes an existing key value pair so maybe have a special sequence for blank I use '_B_l_a_n_k_'
{code}
var
  SList: TStringList;
  procedure SetVal( _key, _val : string );
  begin
     if _val.IsEmpty then
       SList.Values[ _key, '_B_L_a_N_K_' );
     else
       SList.Values[ _key,_val  );
  end;

begin
  SList: TStringList.create; 
  SetVal( 'Test', ''); 
  setVal( 'This', 'out here' );  
end;
{code}

Space in the string works fine for me !!!




--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 5:49 PM EST
From: Linden ROTH