Jump to content
Sign in to follow this  
Henry Olive

Alfabetical order of a letter

Recommended Posts

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

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 by Henry Olive

Share this post


Link to post
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
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

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
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
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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×