Home > Net >  C # winform window procedure in the string conversion to type int always prompt anomalies
C # winform window procedure in the string conversion to type int always prompt anomalies

Time:11-23

New established a c # winform procedure, will be converted to string type int is always abnormal format, when tried
Int. J the parse (), and the convert toint32 () will be wrong, when the same in the console program code without exception, could you tell me why?
[code
Private void button1_Click (object sender, EventArgs e)
{
If (txt1. Text. The Trim ()!="")
{
String STR=txt1. Text;//input ten Numbers, commas between
String [] STRS=STR. The Split (', ');
Int [] array=new int [STRS. Length];
for (int i=0; i{
Array [I]=the Convert. ToInt32 (STRS [I]);//is it always prompt the unusual
}
Txt2. Text=array [0]. ToString ();






}
} [/code]

CodePudding user response:

 
,2,3,4,5,6,7,8,9,11 string atxt="1";
Int [] aaa=atxt. The Trim (). The Split (', '). The Select (a=& gt; Int. Parse (a)). ToArray ();
Atxt=aaa [0]. ToString ();

CodePudding user response:

Do you want to see you is to control the contents of the equivalent int, you input can be converted to type int losing, 1, 1 May, for example, you enter 1, a is no good, cannot be converted into a plastic only

CodePudding user response:

May be entered in Chinese comma, and segmentation is used in English, a breakpoint, debugging see split after the contents of the array;

CodePudding user response:

Your debugged
String [] STRS=STR. The Split (', ', ', ');
May be you have a problem here

CodePudding user response:

Sleep
references the building Lord reply:
new established a c # winform procedure, will be converted to string type int, abnormal format are always tried
Int. J the parse (), and the convert toint32 () will be wrong, when the same in the console program code without exception, could you tell me why?
[code
Private void button1_Click (object sender, EventArgs e)
{
If (txt1. Text. The Trim ()!="")
{
String STR=txt1. Text;//input ten Numbers, commas between
String [] STRS=STR. The Split (', ');
Int [] array=new int [STRS. Length];
for (int i=0; i{
Array [I]=the Convert. ToInt32 (STRS [I]);//is it always prompt the unusual
}
Txt2. Text=array [0]. ToString ();






}
} [/code]

Is likely to last comma is not for you, cause problems in the
For example,
6,

CodePudding user response:

Txt1. Text. The Trim ()

This value can be posted to see, the estimate is the problem here!

CodePudding user response:

With string to trim down the first null character upstairs,
Split also have parameters (StringSplitOptions RemoveEmptyEntries) prevent the input entered can ignore empty, no input data
  •  Tags:  
  • C#
  • Related