刚自学,碰到了这个问题
读取txt文本,内容大概这样:
张三,200
王五,300
赵六,400就是让取出后面的字符,转成int进行比较,输出最大值。我知道用split分割出来,转化成数字,也知道该怎么比较。可是不知道该怎么用for或者foreach弄啊。

解决方案 »

  1.   

            string str = @"张三,200
                                 王五,300
                                  赵六,400";
            ArrayList list = new ArrayList();
            Match mc = Regex.Match(str, @"\d+");
            while (mc.Success)
            {
                list.Add(mc.Value);
                mc = mc.NextMatch();
            }
            string[] array = list.ToArray(typeof(string)) as string[];
            array.OrderByDescending(n => int.Parse(n)).Take(1).ToList().ForEach(n => Response.Write(n));
      

  2.   

    文件放在C:\Users\tzg\Desktop\try.ini
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"C:\Users\tzg\Desktop\try.ini";
                if (File.Exists(path))
                {
                    Console.WriteLine("in");
                    StreamReader sr = new StreamReader(path, true);
                    string[] data=new string[3];
                    int i=0;
                    while(i<3)
                    {
                        data[i]=sr.ReadLine();
                        i++;
                    }
                    foreach (string s in data)
                    {
                        char[] a = { ','};
                        string[] t =new string[4];
                        t= s.Split(a);
                        Console.WriteLine(t[1]+"\n"+t.Length);
                    }
                    Console.ReadLine();
                }
            }
        }
    }
      

  3.   


    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getMaxFromTextFile();
            }
        }    public int getMaxFromTextFile()
        {
            int max = 0;
            ArrayList al = new ArrayList();
            //
            FileStream fs = new FileStream(Server.MapPath("test.txt"), FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string str = sr.ReadLine();
            for (int i = 0; str != null; i++)
            {
                al.Add(str);
                str = sr.ReadLine();
            }
            string s = string.Empty;
            for (int j = 0; j < al.Count; j++)
            {
                s += al[j] + ";";
            }
            string[] ss = new string[al.Count];
            ss = s.Split(';');
            //接受整形数组
            int[] numbers = new int[ss.Length];
            for (int k = 0; k < ss.Length; k++)
            {
                numbers[k] = int.Parse(ss[k].Split(',')[1]);
            }
            //LINQ取得最大值
            var result = numbers.Select(p => p).Max();
            max = int.Parse(result.ToString());
            sr.Close();
            fs.Close();
            return max;
        }
      

  4.   

    上面的有问题,测试了一下,下面的可用txt文件和你的页面放在同一个目录下using System.IO;
    using System.Text;
    using System.Collections;
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Response.Write(getMaxFromTextFile().ToString());
            }
        }    public int getMaxFromTextFile()
        {
            int max = 0;
            string str = string.Empty;
            ArrayList al = new ArrayList();
            //
            FileStream fs = new FileStream(Server.MapPath("test.txt"), FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            while ((str = sr.ReadLine())!=null)
            {
                al.Add(str);
            }
            string s = string.Empty;
            for (int j = 0; j < al.Count; j++)
            {
                s += al[j] + ";";
            }
            //取出最后一个;
            s = s.TrimEnd(';');
            string[] ss = new string[al.Count];
            ss = s.Split(';');
            //接受整形数组
            int[] numbers = new int[ss.Length];
            for (int k = 0; k < ss.Length; k++)
            {
                numbers[k] = int.Parse(ss[k].Split(',')[1]);
            }
            //LINQ取得最大值
            var result = numbers.Select(p => p).Max();
            max = int.Parse(result.ToString());
            sr.Close();
            fs.Close();
            return max;
        }
      

  5.   

    test.txt文件如下:张三,200
    王五,300
    赵六,400
    田七,1200
    刘八,300
    钱九,400