各位帮忙解决个foreach的小问题啊
刚自学,碰到了这个问题读取txt文本,内容大概这样:
张三,200
王五,300
赵六,400
就是让取出后面的字符,转成int进行比较,输出最大值。
我知道用split分割出来,转化成数字,也知道该怎么比较。可是不知道该怎么用for或者foreach弄啊。 --------------------编程问答--------------------
string str = @"张三,200--------------------编程问答-------------------- 文件放在C:\Users\tzg\Desktop\try.ini
王五,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));
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();
}
}
}
}
--------------------编程问答--------------------
--------------------编程问答-------------------- 上面的有问题,测试了一下,下面的可用
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;
}
txt文件和你的页面放在同一个目录下
--------------------编程问答-------------------- ddddddddddddddddddddddddddddd --------------------编程问答-------------------- 你现在做到哪里了 是不是得到一个字符串了 --------------------编程问答-------------------- test.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;
}
张三,200
王五,300
赵六,400
田七,1200
刘八,300
钱九,400
补充:.NET技术 , C#