How to convert a certain string[index] to int in C#?

。_饼干妹妹 提交于 2021-01-28 07:10:12

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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

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




回答5:


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.




回答6:


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




回答7:


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';
  ...
}


来源:https://stackoverflow.com/questions/42001743/how-to-convert-a-certain-stringindex-to-int-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!