# 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

ord('C')-ord('A')+1, ORD('E')-ORD('A')+1, ...

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

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

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?

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

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

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?

A-Z. AA-ZZ, AAA-ZZZ

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

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.