Trouble seperating text box input into individual numbers

Started by alkis21, Sat 14/11/2020 23:41:11

Previous topic - Next topic

alkis21

Hello, I know the title is not very clear but I could not think of a way of describing the problem in one phrase.

I have a GUI that requires the player to type a code. Let's say that the player types 241.

What I want to do is to identify the numbers that the player types, and run some mathematical operations with them.

I can tell what the player typed as first, second, third number by using the Chars[] property on the string that is the GUI input. So I know that Chars[0]=1, Chars[1]=4 and Chars[2]=2. But these are defined as char, which I can neither convert to int nor make them strings so that I can use the AsInt property.

How can I identify each of the characters as int?

Any help will be appreciated.

Crimson Wizard

#1
What you could do is to create a string out of single char and then convert that string to int:

Variant 1:
Code: ags

String snum1 = fullstring.Substring(0, 1);
String snum2 = fullstring.Substring(1, 1);
String snum3 = fullstring.Substring(2, 1);
int num1 = snum1.ToInt();
// etc


Variant 2:
Code: ags

char c1 = fullstring.Chars[0];
String snum1 = String.Format("%c", c1);
int num1 = snum1.ToInt();
// etc



And there is also Variant 3: it's a trick you can do, knowing that number characters have sequential codes in ASCII table, you basically subtract character of '0' from your one char number:
Code: ags

char c1 = fullstring.Chars[0];
int num1 = c1 - '0';

alkis21

Thank you for your quick reply. I tried Variant 2 and it worked, although I had to use .AsInt instead of ToInt().

SMF spam blocked by CleanTalk