Henry Olive 5 Posted July 11, 2021 Is there an easy way to get Alfabetical (English) order number of a letter like C=3 , E=5 , H=8 .... Thank You Share this post Link to post
Lajos Juhász 293 Posted July 11, 2021 ord('C')-ord('A')+1, ORD('E')-ORD('A')+1, ... Share this post Link to post
Henry Olive 5 Posted July 11, 2021 (edited) Thank you Lajos, if Edit1.Text ='A' then // (Edit1.Text =D) Edit2.Text := '1' else Edit2.Text := IntToStr(ord(Edit1.Text) - ord('A')+1); Expecting 4 but getting 37266172 with below code i get correct result ShowMessage(IntToStr(Ord('D') - Ord('A')+1)); Edited July 11, 2021 by Henry Olive Share this post Link to post
Vandrovnik 214 Posted July 11, 2021 28 minutes ago, Henry Olive said: Thank you Lajos, if Edit1.Text ='A' then // (Edit1.Text =D) Edit2.Text := '1' else Edit2.Text := IntToStr(ord(Edit1.Text) - ord('A')+1); Expecting 4 but getting 37266172 with below code i get correct result ShowMessage(IntToStr(Ord('D') - Ord('A')+1)); Edit1.Text is string, not char. Try Edit1.Text[1] (and do not forget to test length of the text first). Share this post Link to post
eivindbakkestuen 47 Posted July 13, 2021 On 7/11/2021 at 9:55 PM, Henry Olive said: Is there an easy way to get Alfabetical (English) order number of a letter like C=3 , E=5 , H=8 .... What exactly are you trying to do? Share this post Link to post
Henry Olive 5 Posted July 13, 2021 Thank you Vandrovnik, eivind From an Excel Sheet i get column name ( for example F ) then i need to convert F to an integer number ( which is 6 ) so that i can import some datas from excel to my dataset. Str:=Edit1.Text; // Which is D Num := Ord(PChar(Str)) - Ord('A')+1; Edit2.Text := IntToStr(Num); I'm getting 37266172 instead of 4 Share this post Link to post
Vandrovnik 214 Posted July 13, 2021 1 hour ago, Henry Olive said: Thank you Vandrovnik, eivind From an Excel Sheet i get column name ( for example F ) then i need to convert F to an integer number ( which is 6 ) so that i can import some datas from excel to my dataset. Str:=Edit1.Text; // Which is D Num := Ord(PChar(Str)) - Ord('A')+1; Edit2.Text := IntToStr(Num); I'm getting 37266172 instead of 4 Num := Ord(Str[Low(Str)]) - Ord('A') + 1; Or, if you are sure your strings begin at offset 1: Num := Ord(Str[1]) - Ord('A') + 1; Share this post Link to post
David Heffernan 2345 Posted July 13, 2021 1 hour ago, Henry Olive said: Thank you Vandrovnik, eivind From an Excel Sheet i get column name ( for example F ) then i need to convert F to an integer number ( which is 6 ) so that i can import some datas from excel to my dataset. Str:=Edit1.Text; // Which is D Num := Ord(PChar(Str)) - Ord('A')+1; Edit2.Text := IntToStr(Num); I'm getting 37266172 instead of 4 Wbat if there are more than 26 columns? Share this post Link to post
Henry Olive 5 Posted July 13, 2021 Thank you so much Vandrovnik, David, Lars My excel sheet always has 12 columns no more Vandrovnik, with below code I get Low cannot be applied to a long string err.msg. Str := Edit1.Text; // Edit1..Text:='D' Num := Ord([Low(Str)]) - Ord('A') + 1; Edit2.Text := IntToStr(Num); But below code WORKS Num := Ord(Str[1]) - Ord('A') + 1; Thank you so much Share this post Link to post
David Heffernan 2345 Posted July 13, 2021 1 hour ago, Henry Olive said: But below code WORKS Num := Ord(Str[1]) - Ord('A') + 1; You should check that Length(Str)=1. 1 hour ago, Henry Olive said: Num := Ord([Low(Str)]) - Ord('A') + 1; This fails of course, it doesn't compile. But it's not the code that was suggested. You should take more care when copying code. Share this post Link to post