+ 2
Convert without returning ASCII value
Is there a simpler approach to convert a string or character to an integer without getting there ASCII value? https://code.sololearn.com/cb0CcjJFpt9R/?ref=app
4 Antworten
+ 2
You can use casting...
string a = "s=8";
for (int i = 0; i < a.Length; i++)
Console.WriteLine(a[i]+" : " + (int)a[i]);
edit:
oh. you need 8 as count then
string a = "s=8";
int count = a[2]-48;
for (int i = 0; i < count ; i++)
Console.WriteLine( a[0] );
+ 2
Jayakrishna 🇮🇳 Yeah it was my fault. Sorry for the misunderstanding.
+ 2
use Char.GetNumericValue:
int count = (int)Char.GetNumericValue(a[2]);
or keep it as a string using Substring:
int count = int.Parse(a.Substring(2));
or Range Operator:
int count = int.Parse(a[2..]);
+ 1
Bob_Li It's interesting how the Substring method returns a string even if it only has one character. Thank you for providing more ways.