简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM