随机读取 TXT 行内容

黄粱一梦 2010-10-14 01:49:00
ASP.NET 如何能实现 随机读取 一个 txt 文件内的 某行 内容 输出 呢????
...全文
275 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
君望永远 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]

string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
string cols = rows[0];
[/Quote]UP
但文本大了 是不是影响性能?
panw520 2010-10-14
  • 打赏
  • 举报
回复
string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
Random rnd=new Random();
string s=rows[rnd.Next(0,rows.Count-1)];
黄粱一梦 2010-10-14
  • 打赏
  • 举报
回复
行 82: Random rnd = new Random();
行 83: string s = XArray[rnd.Next(0, XArray.Count - 1)];
cjh200102 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 myhope88 的回复:]

既然可以读取行,就那个行索引随机取下就行了,不过要注意边界问题
[/Quote]
支持楼上
cxq13178000126 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 myhope88 的回复:]
string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
Random rnd=new Random();
string s=rows[rnd.Next(0,rows.Count-1];[/Quote]

这样 刚发错了不好意思
cxq13178000126 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 myhope88 的回复:]
既然可以读取行,就那个行索引随机取下就行了,不过要注意边界问题
[/Quote]

这样就可以了吧
wuyq11 2010-10-14
  • 打赏
  • 举报
回复
string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
Random rnd=new Random();
string s=rows[rnd.Next(0,rows.Count-1];
myhope88 2010-10-14
  • 打赏
  • 举报
回复
既然可以读取行,就那个行索引随机取下就行了,不过要注意边界问题
alton466 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jshi123 的回复:]
var lines = File.ReadAllText(@"d:\test.txt").Split('\n');
string xx = lines[99]; // 取第100行
[/Quote]

加个随机函数,设置范围即可读取范围内的行的内容。
黄粱一梦 2010-10-14
  • 打赏
  • 举报
回复
没看到哪里出现随机参数呢 ??
我这样写也没反应啊

case "笑话":
string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
string cols = rows[0];
Response.Write(cols);
break;
q107770540 2010-10-14
  • 打赏
  • 举报
回复
string[] rows= File.ReadAllLines("D:\\1.txt", Encoding.UTF8);
string cols = rows[0];
jshi123 2010-10-14
  • 打赏
  • 举报
回复
var lines = File.ReadAllText(@"d:\test.txt").Split('\n');
string xx = lines[99]; // 取第100行
黄粱一梦 2010-10-14
  • 打赏
  • 举报
回复
static public String xiaohuas()
{
string[] strArray = System.Text.RegularExpressions.Regex.Split(XiaoHua, "|");
int maxValue = 0;
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[j] == "|")
{
maxValue++;
}
}
Random random = new Random();
string str = strArray[random.Next(0, maxValue)];
return str;
}

我这样写了一个 为什么 只显示第一个内容 不随机显示后面几个呢???
huangwenquan123 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 xx0xx0000 的回复:]
“System.Array”不包含“Count”的定义,并且找不到可接受类型为“System.Array”的第一个参数的扩展方法“Count”(是否缺少 using 指令或程序集引用?)
[/Quote]换成Length
黄粱一梦 2010-10-14
  • 打赏
  • 举报
回复
“System.Array”不包含“Count”的定义,并且找不到可接受类型为“System.Array”的第一个参数的扩展方法“Count”(是否缺少 using 指令或程序集引用?)

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧