1

I have this code:

string number = "124235245423523423", number2 = "3423525232332325423";
for(i=number.length-1;i>=0;i--){
int save = Convert.ToInt32(number[i]) + Convert.ToInt32(number2[i]);
}

That is not the complete code but my question is why can't I convert and access some value at a certain index of a string as an integer? Isn't there a straight forward approach to this? I have tried some things but it didn't work out.

Talha Imam
  • 1,046
  • 1
  • 20
  • 22

7 Answers7

2

You're looking for int.Parse.

int save = int.Parse(number[3].ToString());

Converting a char to Int32 returns the value of that character in the current encoding.

For more information, see the MSDN documentation for Int32.Parse.

Zesty
  • 2,922
  • 9
  • 38
  • 69
  • This works, i understand that since int.Parse and Convert.ToInt32 converts a string to integer, we can first convert a character at a string index into a string and then use the Conversion or Parsing methods over them. Makes sense – Talha Imam Feb 03 '17 at 04:45
  • @TalhaImam Great! If the answer meets your requirement, could you please click on the tick mark on the left side to accept it as the answer to your question? That will help other people who search for the same thing in the future. – Zesty Feb 05 '17 at 06:27
1

I think that this is what you are looking for. You have to access the string by the indexer value and then convert:

int yourNumber = Convert.ToInt32(number[4].ToString());

This will give you value 3

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • 1
    It will not work. `Convert.ToInt32(Char)` returns symbol code (i.e. `Convert.ToInt32("1")` returns `49`, not `1`). – Roman Feb 02 '17 at 12:07
0

Here is one way to do it:

string number = "124235245423523423", number2 = "3423525232332325423";
for(int i=number.Length-1;i>=0;i--){
    int save = int.Parse(number[i].ToString()) + int.Parse(number2[i].ToString());
    Console.WriteLine(save);
}

When Convert.ToInt32() gets a char, it returns the UTF-16 encoded code unit of the value argument (read on MSDN).
On the other hand, when it gets a string, it returns the number that string contains, or throws an exception if it's not a number.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0
string number = "124235245423523423", number2 = "3423525232332325423";
    for(int i=number.Length-1;i>=0;i--){
    int sum = int.Parse(number[i].ToString()) + int.Parse(number2[i].ToString());
    Console.WriteLine(sum);

working example

0

I believe the String.ToCharArray is what you're looking for:

https://msdn.microsoft.com/en-us/library/2c7h58e5(v=vs.110).aspx

raul7
  • 171
  • 1
  • 10
0

Well, since number[i] is of type char you can convert it into corresponding int either by (the most general case)

 int result = (int) char.GetNumericValue(number[i]);

please notice, that char.GetNumericValue returns double, it's actual for, say, '⅜' character (3/8 corrsponds to 0.375); or if you work with ['0'..'9'] range only, all you have to do is to subtract '0':

 int result = number[i] - '0';

So far your code can be implemented as

 // Math.Min - number.Length and number2.Length have diffrent size
 // - 1      - strings are zero-based [0..Length - 1]
 for (i = Math.Min(number.Length, number2.Length) - 1; i >= 0; i--) {
   int save = number[i] + number2[i] - 2 * '0';
   ...
 }

probably you want to right align both strings:

 number  =  "124235245423523423"
    +
 number2 = "3423525232332325423"
 ------------------------------
            354...       ...846

in this case you have to modify for loop:

for (int i = 0; i < Math.Max(number.Length, number2.Length); ++i) {
  int sum = 
    (i < number.Length ? number[number.Length - i - 1] : '0') +
    (i < number2.Length ? number2[number2.Length - i - 1] : '0') -
     2 * '0';
  ...
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

I needed a value at a certain index, here's how i did it and it works wonderfully for me:

int number = (int)char.GetNumericValue(number[index])

What I actually did is that i converted the character at the index i needed into a double first through GetNumericValue method and then i converted that double into an integer.

Talha Imam
  • 1,046
  • 1
  • 20
  • 22