C# how to get char - 'A'

I am taking a string "str" full of capitals 'A' thru 'Z' which represents a 26 base number where 'A' = 0, 'B' = 1, and so on.....

part of this operation is a line like so:  

int place_number =  (int)(str[i] - 'A') * (26*(i))

Open in new window

essentially I just want the numerical difference between a character in the string and 'A', so that 'A' returns 0, 'B' returns 1, and so on....

I am more familiar with C++ that C#, so I'm not understanding the type conversion here.  The compiler keeps saying Error      1      A new expression requires (), [], or {} after type      

I don't understand that.  

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LindyGeekAuthor Commented:
So, how do I make the compiler happy and still get the result of str[i] - 'A'   ?
Shaun KlineLead Software EngineerCommented:
Maybe this:
int place_number = (((int)str[i]) - ((int)'A')) * (26 * (i));

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

this is because you defined str as a string array, you should define it as a char array

int i=0;
            char[] str =
            int place_number = (int) (str[i] - 'A')*(26*(i));

Open in new window

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

LindyGeekAuthor Commented:
Cumbersome, but that did it!
LindyGeekAuthor Commented:
Sorry elimesika,

looks like I gave the solution credit as you were posting...
Hi friend,

Welcome to C# world! ;)

try this:

Convert.ToInt32(str[i] - 'A');

that's ok, I am here to learn , not for the points ;-)
Btw, a string is indeed an array of char!


            string myString = "abcdefghijklmnopqrstuvwxyz";
            Console.WriteLine("char at pos {0}:{1}", 10, myString[10]);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.