Solved

C#    how to get char - 'A'

Posted on 2010-11-10
8
1,161 Views
Last Modified: 2012-05-10
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.  


0
Comment
Question by:LindyGeek
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:LindyGeek
ID: 34105507
So, how do I make the compiler happy and still get the result of str[i] - 'A'   ?
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 80 total points
ID: 34105636
Maybe this:
int place_number = (((int)str[i]) - ((int)'A')) * (26 * (i));
0
 
LVL 19

Expert Comment

by:elimesika
ID: 34105717
HI

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

int i=0;
            char[] str =
            {
                'A',
                'B',
                'C'
            }
            ;
            int place_number = (int) (str[i] - 'A')*(26*(i));

Open in new window

0
 

Author Closing Comment

by:LindyGeek
ID: 34105728
Cumbersome, but that did it!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:LindyGeek
ID: 34105737
Sorry elimesika,

looks like I gave the solution credit as you were posting...
0
 
LVL 3

Expert Comment

by:yanoch
ID: 34105789
Hi friend,

Welcome to C# world! ;)

try this:

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

Expert Comment

by:elimesika
ID: 34105872
LindyGeek

that's ok, I am here to learn , not for the points ;-)
0
 
LVL 3

Expert Comment

by:yanoch
ID: 34106444
Btw, a string is indeed an array of char!

exemple:

[code]
            string myString = "abcdefghijklmnopqrstuvwxyz";
            Console.WriteLine("char at pos {0}:{1}", 10, myString[10]);
[/code]
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now