+ 2

Selectivly choose from user input?

If I have the Console.ReadLine() method, and I want to separate between int and string whatever the user writes (or whatever I want to take from it), is it possible? For example: User imputs: "Age is 23", can I separate the string "Age is..." from "23" and turn the last one into an integer?

3rd Jul 2016, 1:26 AM
MrSuperTebby
MrSuperTebby - avatar
7 Respuestas
+ 2
if u convert text value to int and it have a alphabetic data or anything expect number it show error text=1234 int=1234 --- text=1234a int=error
3rd Jul 2016, 9:55 PM
Artemis Ariamehr
Artemis Ariamehr - avatar
+ 1
You can probably use int.TryParse method to get int from a string without running into error. But if you need to parse the string you should use string.indexof() to find specific characters.
10th Jul 2016, 1:30 AM
Ahmad Horrabadi
Ahmad Horrabadi - avatar
+ 1
well, you can do that. You can first do the int.TryParse, get the int in the string, then do a search on the inputted string to get the position of the int in the string, then u can just trim off the string to that position. i believe this should work.
14th Sep 2016, 7:40 PM
Maduekwe Ebuka Collins
Maduekwe Ebuka Collins - avatar
0
Thanks! Also, can I asign 2 variables like this: string text = (whatever he wrote) int age = (the conversion to int) And then just work with them that way?
3rd Jul 2016, 1:39 AM
MrSuperTebby
MrSuperTebby - avatar
0
when u get a number convert it to int int i=convert.toint(console.read( ) ); then u can add string str="age is"+i.tostring( ) ; or if u instead get int number in string value u can use diferent ways like get it from char index every string value is array of chars it began from 0 to string length-1 or use split command or other ways
3rd Jul 2016, 9:51 PM
Artemis Ariamehr
Artemis Ariamehr - avatar
0
you must have 3 or 4 variable one of them is for string one & of them for int another one for char(to get char code and find whom char is number or string) step 1=input_variable(string) step 2=get string text length step 3=repeat cycle by text length step 4=get 1 character of input_variable and convert it to char step 5=char_variable and main function(most important. depend on what charcode is it find it is number or not ) step 6= you can use temporary stringvariable then convert it to int or define what char equal than what number is step 7 =as you want.as you wish
20th May 2018, 5:57 AM
Artemis Ariamehr
Artemis Ariamehr - avatar
0
Need separate age from text just by char code and char index
1st Mar 2019, 10:08 PM
Artemis Ariamehr
Artemis Ariamehr - avatar