Search code examples
delphidata-structurespascal

Is there any way to convert a String variable into a Char


I'm asking if is there a way to convert a unit of a string into a char? to be more clear, is there a way to legalize this instruction:

var
    s : String;

begin
    s:='stackoverflow';
    ord(s[1]); // Illegal expression    
end.    

Getting the ASCII code from one unit of a string ( which has to be a char ) demanded a complicated algorithm in Object Pascal, can I do it in an easier way?


Solution

  • You can't use Ord(s[1]) as its own statement like you are, but you can use it as an expression within a larger statement, eg:

    var
      s : String;
      i: Integer;
    begin
      s := 'stackoverflow';
      i := Ord(s[1]); // i = 115
    end.