我试过C# vs.net上所有的将字符串转换为整数型的方法 就是不行啊!!  请大家帮帮忙!!急啊!!
  下面是我的代码
    string path1 = @"e:\Dupout";
            string path2 =path1+ @"\dupout.txt";
            //创建文件目录
            if (!Directory.Exists(path1))
            {
                Directory.CreateDirectory(path1);
            }
            //创建文件
            if (!File.Exists(path2))
            {
                File.Create(path2);
            }
            //把文本文件写入dupout.txt文件
            using (StreamWriter sw = File.CreateText(path2))
            {
                sw.WriteLine("姓名\t性别\t年龄\t工资\t");
                sw.WriteLine("刘洋成\t男\t18\t2354\t");
                sw.WriteLine("敖翔\t男\t19\t3456\t");
                sw.WriteLine("陈玲\t女\t18\t1234\t");
                sw.WriteLine("杜贤伟\t男\t20\t4456\t");
               
            }
            string text = null;
            using (StreamReader sr = File.OpenText(path2))//path2= @"e:\Dupout\dupout.txt";
            {
                while (sr.Peek()>=0)
                {
                    text+=sr.ReadLine();//把文件中的数据读入text
                }
               
            }
            string[] words = text.Split('\t','\n');            int[] arr = new int[4];
            int j=0;
            for (int i = 8; i < words.Length; i = i + 4)
            {
               
                arr[j] = Convert.ToInt32(words[i]);//??????? 把words[i]转换为整数类型????            }
            foreach (int n in arr)
            {
                Console.Write(n + "\t");
            }

解决方案 »

  1.   

    int temp;
    if(Int32.TryParse(words[i],temp))
       arr[j] = temp;
      

  2.   

    int temp; 
    if(Int32.TryParse(words[i],temp)) 
          arr[j]   =   temp;
      

  3.   

    LZ是不是把循环的索引给搞错了?
       for   (int   i   =   8;   i   <   words.Length;   i   =   i   +   4) 
       { 
           arr[j]   =   Convert.ToInt32(words[i]);//???????   把words[i]转换为整数类型????                         
       } 
    如果把int   i   =   8;改成 int i=6则可以取出年龄的值。
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;namespace ConsoleApplication69
    {
        class Program
        {
            static void Main(string[] args)
            {            string path1 = @"e:\Dupout";
                string path2 = path1 + @"\dupout.txt";
                //创建文件目录 
                if (!Directory.Exists(path1))
                {
                    Directory.CreateDirectory(path1);
                }
                //创建文件 
                if (!File.Exists(path2))
                {
                    File.Create(path2);
                }
                //把文本文件写入dupout.txt文件 
                using (StreamWriter sw = File.CreateText(path2))
                {
                    sw.WriteLine("姓名\t性别\t年龄\t工资\t");
                    sw.WriteLine("刘洋成\t男\t18\t2354\t");
                    sw.WriteLine("敖翔\t男\t19\t3456\t");
                    sw.WriteLine("陈玲\t女\t18\t1234\t");
                    sw.WriteLine("杜贤伟\t男\t20\t4456\t");            }
                string text = null;
                using (StreamReader sr = File.OpenText(path2))//path2=   @"e:\Dupout\dupout.txt"; 
                {
                    while (sr.Peek() >= 0)
                    {
                        text += sr.ReadLine();//把文件中的数据读入text 
                    }            }
                string[] words = text.Split('\t', '\n');            int[] arr = new int[4];
                for (int i = 7, j = 0; i < words.Length; i = i + 4)
                {
                    arr[j++] = int.Parse(words[i]);
                }
                foreach (int n in arr)
                {
                    Console.Write(n + "\t");
                }
            }
        }
    }就是循环搞错了
      

  5.   

    用int.Parse或者int.TryParse
    前者在失败的时候抛异常,后者不抛
      

  6.   

    用 int.Parse。
    但是必须满足要求MSDN上的:public static int Parse(string s);s 参数包含一个如下形式的数字:[ws][sign]digits[ws]方括号([ 和 ])中的项是可选的;其他项的值如下所示。 ws 
    可选的空白。 
    sign 
    一个可选符号。 
    digits 
    一系列从 0 到 9 之间的数字。异常:ArgumentNullException : s 为空引用(Visual Basic 中为 Nothing)。 
    FormatException:  s 不是仅由一个可选负号后跟一系列从 0 到 9 的数字组成的。 
    OverflowException :  s 表示小于 MinValue 或大于 MaxValue 的数字。 
      

  7.   

    arr[j]      =     Int32.Parse(words[i],10);
      

  8.   

    可以先将你的字串转化为unicode编码
    string s = "您好";
            string num = String.Empty; //这里用string来展示10进制任意长字串
            string numx = String.Empty;//16进制
            Byte[] bt = System.Text.Encoding.Unicode.GetBytes(s);
            foreach (Byte b in bt)
            {
                num += b.ToString();
                numx += b.ToString("x");
            }
            Response.Write(num+numx);
      

  9.   

                try
                {
                    string path1 = @"C:\Dupout";
                    string path2 = path1 + @"\dupout.txt";
                    
                    //创建文件目录
                    if( !Directory.Exists( path1 ) )
                    {
                        Directory.CreateDirectory( path1 );
                    }
                    //创建文件
                    if( !File.Exists( path2 ) )
                    {
                        FileStream stream = File.Create( path2 );
                        stream.Dispose();
                    }
                    //把文本文件写入dupout.txt文件
                    using( StreamWriter sw = File.CreateText( path2 ) )
                    {
                        sw.WriteLine( "姓名\t性别\t年龄\t工资\t" );
                        sw.WriteLine( "刘洋成\t男\t18\t2354\t" );
                        sw.WriteLine( "敖翔\t男\t19\t3456\t" );
                        sw.WriteLine( "陈玲\t女\t18\t1234\t" );
                        sw.WriteLine( "杜贤伟\t男\t20\t4456\t" );
                    }
                    string text = null;
                    using( StreamReader sr = File.OpenText( path2 ) )//path2=   @"e:\Dupout\dupout.txt";
                    {
                        while( sr.Peek() >= 0 )
                        {
                            text += sr.ReadLine();//把文件中的数据读入text
                        }                }
                    string[] words = text.Split( '\t', '\n' );                int[] arr = new int[ 4 ];
                    int j = 0;
                    for( int i = 6; i < words.Length; i = i + 4 )
                    {                    arr[ j ] = Convert.ToInt32( words[ i ] );//???????   把words[i]转换为整数类型????                         }
                        foreach( int n in arr )
                        {
                            Console.Write( n + "\t" );
                        }
                    }
                }
                catch( Exception ex )
                {
                    Console.WriteLine( ex.Message );
                }两个错误,一个是文件流没有关闭,一个是索引位置错了。
      

  10.   

    先判断能否转换成Int型,然后再执行操作
    public bool IsDecimal(string strNum)
    {
    try
    {
    decimal var = Convert.ToDecimal(strNum);
    return true;
    }
    catch
    {
    return false;
    }
    }
    code:string[] words = text.Split('\t', '\n');            int[] arr = new int[4];
                for (int i = 7, j = 0; i < words.Length; i = i + 4)
                {   
                    if(IsDecimal(words[i].tostring()))
                    arr[j++] = int.Parse(words[i]);
                }
                foreach (int n in arr)
                {
                    Console.Write(n + "\t");
                }
      

  11.   

    学到了一个新方法!!
    int32.tryParse