-1

If I have a string of digits and int variables a, b, c and I want to assign the first digit to int a, the second to int b and so on, how do I do that? This snippet illustrates my problem:

var str = "123";
int a = Convert.ToInt32(str[0]);
Console.WriteLine(str[0] + " " + a );

Output:

1 49

What do I do (besides subtract 48 from every attempted conversion)?

EDIT

Today I learned that str[0] produces a Char, not a string.

JB0x2D1
  • 838
  • 8
  • 20
  • Why don't you just use `str` instead of index at 0? – Tdorno Sep 21 '14 at 22:47
  • I don't think I understand. Or maybe you don't understand. I want `int a == 1`, `int b == 2`, `int c == 3`. – JB0x2D1 Sep 21 '14 at 22:49
  • Side note: "string" generally refer to `string` type in C# questions and in your case you are talking about "characters of a string" which are in C# represented by `char` type. Using proper words could either helped you to find solution (i.e. duplicate with more C#/.Net approach than C-style solution you've accepted) or at least help others in answering. – Alexei Levenkov Sep 22 '14 at 00:50
  • Thanks for that. I didn't know that `str[0]` was a `char`. Obviously if I'd known that, I wouldn't have asked the question. I did try to search before asking... – JB0x2D1 Sep 22 '14 at 00:55

3 Answers3

3
int a = str[0] - '0';
int b = str[1] - '0';
int c = str[2] - '0';

etc...

This works because str[0] is a char, and so is '0', and subtracting chars gives you an int in C#.

P.S. Well, strictly speaking, it's not that subtracting chars gives you an int. Subtraction is not defined for char, but chars are implicitly convertible to int, so that's what happens: two implicit conversions into int, and those can obviously be subtracted from each other to give another int.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • +1 for C-style solution. For .Net one check [char to int](http://stackoverflow.com/questions/239103/c-sharp-char-to-int). Strictly speaking this is exactly "besides subtract 48 from every attempted conversion" from the question... – Alexei Levenkov Sep 22 '14 at 00:51
  • Well... I consider a cast of `char` to `int` to be C#-style, and I consider *parsing* a *single-character* number neither a good idea nor more idiomatic in C#. – Roman Starkov Sep 22 '14 at 01:30
  • At the end of the day, in my opinion, this is more elegant than `int a = Char.GetNumericValue(str[0]);` – JB0x2D1 Sep 22 '14 at 01:50
0

You are using the Convert.ToInt32(char) overload. Try with ToString:

int a = Convert.ToInt32(str[0].ToString());

Another way using Math (which I'm not really good at)

string x = "123";
int number = int.Parse(x);
int first = number / 100;
int second = (number % 100) / 10;
int third = number % 10;
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

To build on Selman22's answer:

var str = "123";
int digit = GetDigit(str, 1);
Response.Write(digit.ToString()); // Prints '1'

int GetDigit(String s, Int32 digitPos)
{
  var actualDigitPos = (s.Length - digitPos) + 1;
  var number = Int32.Parse(s);

  return (number % (Int32)Math.Pow(10, actualDigitPos)) / (Int32)Math.Pow(10, actualDigitPos - 1);
}

Keep in mind, you should add some sort of validation on string length and digitPos, as well as a valid String, etc.

Theofanis Pantelides
  • 4,724
  • 7
  • 29
  • 49