Kpt.Khaos
Kpt.Khaos

Reputation: 693

How to apply indexing for expression of type int c#

I have to split a value example 44 I need the second 4. I have a way to so so now I just can figure how to make int index-able. I can make it indexable using string but I need it in int. How can I solve this issue?

Conversion that does not work

int secondDigit = num;
ld.ScaleGroup = (ScaleGroup)Convert.ChangeType(secondDigit, typeof(ScaleGroup));

int num = Convert.ToInt32(ld.ScaleGroup);
char secondDigit = num[1];//here is the issue

Upvotes: 0

Views: 1695

Answers (1)

Habib
Habib

Reputation: 223422

You can try:

int secondDigit = (int) char.GetNumericValue(num.ToString()[1]);

But a simpler option would be:

int secondDigit1 = num % 10;

If Id.ScaleGroup is a string then you can directly access its value at index 1, but remember to check the Length before accessing index.

The simpler option (involving % division) would only work for two digit numbers.

Upvotes: 2

Related Questions