33 Votes

Delphi/Lazarus: Convert String to Char

Question by Guest | Last update on 2022-12-07 | Created on 2018-01-02

I have a string that I would like to convert to a char, because I can only pass it to a function in this format.

Although my string only consists of a single letter, which actually corresponds to a char, the function does not accept my input. Is there any way to change the string to a variable of the type Char? Something like c := s ist not possible and results in an error message.

ReplyPositiveNegative
6Best Answer6 Votes

Similar to an array, you can also access the individual parts of a string.

So for example:

var
  c: char;
  s: string;
begin
  s := 'A';
  c := s[1];
end;

Here you have a string with the content "A", which we convert to a char with the content "A".

With s[i] you get the letter at the corresponding position i of s. s[1] would be the first letter of s, because unlike an array, the counting of a string does not begin at 0 but starts with 1.
Last update on 2022-12-07 | Created on 2018-01-02

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.