Board index » delphi » Null-terminated to Un-Null-terminated?

Null-terminated to Un-Null-terminated?

How do I change a Null-terminated string to an Un-Null-terminated string?

I'm using the following code.
Var
    cString : String;
Begin
cString := JournalStrings[nItem]; {This is a TStringList}
cString := Copy(cString,11,Length(cString) - 10);
end;

cString Contains 'J:\MYFILE\STUFF.TXT' when It's set here.  As you can see,
the ACTUAL length of the string is 19, but when I ask it what the
Length(cString) is, it returns 20.  I assume that it is because there is a
Null character at the end.  How do I get rid of it?  I can't simply delete
the last character, 'cause it changes the string to 'J:\MYFILE\STUFF.TX',
deleting the actual last character.

Thanks for any help

Jesse

 

Re:Null-terminated to Un-Null-terminated?


Quote
Jesse Castleberry wrote:
> How do I change a Null-terminated string to an Un-Null-terminated string?

> I'm using the following code.
> Var
>     cString : String;
> Begin
> cString := JournalStrings[nItem]; {This is a TStringList}
> cString := Copy(cString,11,Length(cString) - 10);
> end;

> cString Contains 'J:\MYFILE\STUFF.TXT' when It's set here.  As you can see,
> the ACTUAL length of the string is 19, but when I ask it what the
> Length(cString) is, it returns 20.  I assume that it is because there is a
> Null character at the end.  How do I get rid of it?  I can't simply delete
> the last character, 'cause it changes the string to 'J:\MYFILE\STUFF.TX',
> deleting the actual last character.

You might look into StrPas and related functions -- converting to and
from String and PChar types. String of course is the expected Pascal
style strings PChar on the other hand is Borland Delphi's implementation
of null terminated strings.

Re:Null-terminated to Un-Null-terminated?


Jesse,

I just tested using your sample string, and Delphi tells me the
Length() is 19; I'm not sure what your problem here is... Can
you post more code?

I tested by dropping three labels on a form, and doing the following
in the Form.OnCreate() handler:

Label1.Caption := 'J:\MYFILE\STUFF.TXT';
Label2.Caption := IntToStr(Length(Label1.Caption));
Label3.Caption := Copy(Label1.Caption, 11, Length(Label1.Caption) -
10);

Label2 displayed 19, and Label3 displayed 'STUFF.TXT'.

BTW, an easier way if you're dealing with filenames is to use the
ExtractFileName() function; it's use would be:

Label3.Caption := ExtractFileName(Label1.Caption);

You'd get the same thing without being as dependent on the exact
position and length of the path portion...

HTH

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Quote
Jesse Castleberry wrote:

> How do I change a Null-terminated string to an Un-Null-terminated string?

> I'm using the following code.
> Var
>     cString : String;
> Begin
> cString := JournalStrings[nItem]; {This is a TStringList}
> cString := Copy(cString,11,Length(cString) - 10);
> end;

> cString Contains 'J:\MYFILE\STUFF.TXT' when It's set here.  As you can see,
> the ACTUAL length of the string is 19, but when I ask it what the
> Length(cString) is, it returns 20.  I assume that it is because there is a
> Null character at the end.  How do I get rid of it?  I can't simply delete
> the last character, 'cause it changes the string to 'J:\MYFILE\STUFF.TX',
> deleting the actual last character.

> Thanks for any help

> Jesse

Re:Null-terminated to Un-Null-terminated?


In article <6h8i8f$44...@news2.ispnews.com>, "Jesse Castleberry"

Quote
<D...@iThink.net> writes:
>cString := Copy(cString,11,Length(cString) - 10);

You're better using :-

cString := Copy(cString,11,Length(cString));

. .  the function only copies to the end of the string, and its one thing less
to change if you change your program. <g>

Alan Lloyd
alangll...@aol.com

Re:Null-terminated to Un-Null-terminated?


In article <6h8i8f$44...@news2.ispnews.com>,
  "Jesse Castleberry" <D...@iThink.net> wrote:

Quote

> How do I change a Null-terminated string to an Un-Null-terminated string?

> I'm using the following code.
> Var
>     cString : String;
> Begin
> cString := JournalStrings[nItem]; {This is a TStringList}
> cString := Copy(cString,11,Length(cString) - 10);
> end;

> cString Contains 'J:\MYFILE\STUFF.TXT' when It's set here.  As you can see,
> the ACTUAL length of the string is 19, but when I ask it what the
> Length(cString) is, it returns 20.  I assume that it is because there is a
> Null character at the end.  How do I get rid of it?  I can't simply delete
> the last character, 'cause it changes the string to 'J:\MYFILE\STUFF.TX',
> deleting the actual last character.

       ??? Delphi tells me the length of that string is 19, not 20. What
makes you think Delphi thinks it's 20?

Showmessage(inttostr(Length('J:\MYFILE\STUFF.TXT')));

Exactly what are you trying to accomplish, and what's going wrong?

David C. Ullrich

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

Re:Null-terminated to Un-Null-terminated?


AlanGLLoyd heeft geschreven in bericht
<1998041812323901.IAA06...@ladder01.news.aol.com>...
|In article <6h8i8f$44...@news2.ispnews.com>, "Jesse Castleberry"

Quote
|<D...@iThink.net> writes:

|
|>cString := Copy(cString,11,Length(cString) - 10);
|
|You're better using :-
|
|cString := Copy(cString,11,Length(cString));
|
|. .  the function only copies to the end of the string, and its one
thing |less to change if you change your program. <g>

Alan:

I've noticed this behaviour of the Copy() function too, but never
dared to use it as a "general rule" in my projects. Does Delphi checks
string boundaries in the Copy() function? IOW, can I be sure doing
this will _never_{*word*222}up into an AV ?

TIA,
Dirk Claessens

Re:Null-terminated to Un-Null-terminated?


In article <6hash6$3v...@xenon.inbe.net>,
  "Dirk Claessens" <Dirk.Claessens@village.#NOSPAM#uunet.be> wrote:

Quote

> AlanGLLoyd heeft geschreven in bericht
> <1998041812323901.IAA06...@ladder01.news.aol.com>...
> |In article <6h8i8f$44...@news2.ispnews.com>, "Jesse Castleberry"
> |<D...@iThink.net> writes:
> |
> |>cString := Copy(cString,11,Length(cString) - 10);
> |
> |You're better using :-
> |
> |cString := Copy(cString,11,Length(cString));
> |
> |. .  the function only copies to the end of the string, and its one
> thing |less to change if you change your program. <g>

> Alan:

> I've noticed this behaviour of the Copy() function too, but never
> dared to use it as a "general rule" in my projects. Does Delphi checks
> string boundaries in the Copy() function? IOW, can I be sure doing
> this will _never_{*word*222}up into an AV ?

         You could try glancing at the docs - this is the sort of
question you _might_ find an answer to in there somewhere. Let's
see, under Copy it says

Description

The Copy function returns a substring of a string.
S is a string-type expression. Index and Count are integer-type expressions.
Copy returns a string containing Count characters starting at S[Index].
If Index is larger than the length of S, Copy returns an empty string.
If Count specifies more characters than are available, the only the characters
from S[Index] to the end of S are returned.

Sure enough.

David C. Ullrich

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

Re:Null-terminated to Un-Null-terminated?


ullr...@math.okstate.edu heeft geschreven in bericht <6hdet9
|
|         You could try glancing at the docs - this is the sort of
|question you _might_ find an answer to in there somewhere. Let's
|see, under Copy it says
[snip]

Well... that's what I _always_ do, as well as pointing out to other
people they should try online help before posting here. It's just that
Copy() is, well... such an ancient, almost _medieval_
Pascal-function...

Bye
Dirk.

Re:Null-terminated to Un-Null-terminated?


Quote
D...@iThink.net ("Jesse Castleberry"),comp writes:
>From: "Jesse Castleberry" <D...@iThink.net>
>Subject: Null-terminated to Un-Null-terminated?
>Date: Fri, 17 Apr 1998 17:39:12 -0400
>cString Contains 'J:\MYFILE\STUFF.TXT' when It's set here.  As you can
>see,
>the ACTUAL length of the string is 19, but when I ask it what the
>Length(cString) is, it returns 20.  I assume that it is because there
>is a
>Null character at the end.  How do I get rid of it?  I can't simply
>delete
>the last character, 'cause it changes the string to
>'J:\MYFILE\STUFF.TX',
>deleting the actual last character.

The REAL number of characters in a string is always one more then you
might think,
since the first entry(actually location 0 [zero]) is the value that
Lenght reads,
because it holds the number of characters in the line.
But if you are 100% sure that there are a Null character in the end,
and that it when
you try to get rid of it, you lose on of your characters, then I'm not
sure if
maybe you have misunderstood something, or if I have...
Anyhowe, tried to Copy() the number of characters you wanted, and seen
if you still are returnd with the same problem?
(This is not a solution, but a way to test if there indeed are an extra
character in the end...kinda...)

//Da -Z-
-<{[Sweden will Rule one's again]}>-

Re:Null-terminated to Un-Null-terminated?


In article <6him19$pe...@xenon.inbe.net>,
  "Dirk Claessens" <Dirk.Claessens@village.#NOSPAM#uunet.be> wrote:

Quote

> ullr...@math.okstate.edu heeft geschreven in bericht <6hdet9
> |
> |         You could try glancing at the docs - this is the sort of
> |question you _might_ find an answer to in there somewhere. Let's
> |see, under Copy it says
> [snip]

> Well... that's what I _always_ do, as well as pointing out to other
> people they should try online help before posting here. It's just that
> Copy() is, well... such an ancient, almost _medieval_
> Pascal-function...

         Yeah, I see your point(???<g>). If I'd realized it was an
ancient function I wouldn't have tried to look it up either...

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

Re:Null-terminated to Un-Null-terminated?


On Mon, 20 Apr 1998 20:54:18 +0200, "Dirk Claessens"

Quote
<Dirk.Claessens@village.#NOSPAM#uunet.be> wrote:

>ullr...@math.okstate.edu heeft geschreven in bericht <6hdet9
>|
>|         You could try glancing at the docs - this is the sort of
>|question you _might_ find an answer to in there somewhere. Let's
>|see, under Copy it says
>[snip]

>Well... that's what I _always_ do, as well as pointing out to other
>people they should try online help before posting here. It's just that
>Copy() is, well... such an ancient, almost _medieval_
>Pascal-function...

String related help has taken a big step down in Delphi 3.  In D2
there was some good help on converting between string types.
For the most part D3 help files are a big improvement, but not
for string related functions.

Carl

Other Threads