Re: read first character in Delphi


[ DelphiLand FAQ ]

Posted by webmaster Guido on July 28, 2004 at 15:11:04:

In Reply to: read first character posted by matt on July 28, 2004 at 00:45:52:

: ok easy i think but im stumped. i read from a text file. the number is 2323. it is displayed in label1.caption. now::::: i want to read the third character in 2323 ..it would be 2. I can understand how to do it but it has to be text. i want to read numbers.

: var
: Initial : string;
: begin
: Name:= 'dd';
: Initial:=Name[1];
: showmessage (Initial);

: this works if label1.caption contains letters not numbers. how do i get the numbers to work.
---------------------

In Delphi, the Caption of a label is of the type "string", so it only can contain a string, not another type like for example an integer number

So, the following two lines can not work, because already the first line would give an error when you try to compile it:

Label1.Caption := 2323; // wrong, 2323 is not a string
Initial := Label1.Caption[1];

But the following is OK:

Label1.Caption := '2323'; // OK, '2323' is a string
Initial := Label1.Caption[1];
If you want to show a number in a caption, you first have to convert it to a string. Example:
var
  Initial : string;
  N: integer;
begin
  N := 2323; 
  // Convert integer N to a string 
  // and show that string in the label
  Label1.Caption := IntToStr(N);
  // Read first character of the Caption
  Initial := Label1.Caption[1];

Does this answer your question?
webmaster Guido


Related Articles and Replies


[ DelphiLand FAQ ]