A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 熊丽 中级黑马   /  2013-8-18 17:13  /  1711 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 熊丽 于 2013-8-20 18:48 编辑
  1. <p> </p><p>using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Text.RegularExpressions;</p><p>namespace test4
  7. {
  8.     class Program
  9.     {
  10.         //4、 文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,
  11.         //每行一个,标题有的长有的短,输出到控制台的时候最多标题长度10,如果超过10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("文本按格式输出:");
  15.             OutputText(@"..\..\..\test4.txt");
  16.             Console.ReadKey();
  17.         }
  18.         protected static void OutputText(string path)//解析文本
  19.         {
  20.             try
  21.             {
  22.                 using (FileStream filestream = new FileStream(path, FileMode.OpenOrCreate))//读取文本
  23.                 {
  24.                     using (StreamReader streamreader = new StreamReader(filestream,Encoding.GetEncoding("gb2312")))
  25.                     {
  26.                         string str = "";
  27.                       <font color="red">  while ((str=streamreader.ReadLine()) != null)//</font>按格式输出 <font color="red"> 可以正确的输出,输出结果为02.png</font>
  28.                         {
  29.                             str = System.Text.RegularExpressions.Regex.Replace(str, "//s+", "");//将多空格替换为一个空格
  30.                             string[] strlist=Regex.Split(str, " ");
  31.                             List<string>LineStream=new List<string>();
  32.                             foreach (string str1 in strlist)//去除数组中为空的值
  33.                             {
  34.                                 if (str1 != null&&str1!="")
  35.                                 {
  36.                                     LineStream.Add(str1);
  37.                                 }
  38.                                     
  39.                             }
  40.                             if (LineStream[0].Length > 10)//长度大于10.则截取
  41.                             {
  42.                                 Console.WriteLine(string.Format("{0}...|{1}", LineStream[0].Substring(0, 8), LineStream[1]));
  43.                             }
  44.                             else
  45.                                 Console.WriteLine(string.Format("{0}|{1}", LineStream[0], LineStream[1]));</p><p>                        }
  46.                     }
  47.                     filestream.Close();
  48.                 }
  49.             }
  50.             catch
  51.             {
  52.             }
  53.         }
  54.     }
  55. }
  56. </p>
复制代码
  1. <p>当改为这样时,输出结果为1.png,只能输出中间一行</p><p><p><font color="red">  while (streamreader.ReadLine() != null)//</font>按格式输出
  2.                         {
  3.                                str = streamreader.ReadLine();</p></p>
复制代码
这是为什么啊,愿各位大神告之

QQ截图20130818171206.png (15.72 KB, 下载次数: 16)

txt内容

txt内容

1.png (12.93 KB, 下载次数: 20)

第一种

第一种

Q2.png (7.22 KB, 下载次数: 18)

Q2.png

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

4 个回复

正序浏览
pm324 发表于 2013-8-20 11:46
你仔细看

你的第一种正确的方法你的第二种方法我们先来分析下你的程序运行流程

对哦,O(∩_∩)O谢谢咯
回复 使用道具 举报
你仔细看

你的第一种正确的方法
  1. while ((str = streamreader.ReadLine()) != null)
  2.                         {
  3.                             str = System.Text.RegularExpressions.Regex.Replace(str, "//s+", "");
复制代码
你的第二种方法
  1. while (streamreader.ReadLine() != null)
  2.                         {
  3.                                str = streamreader.ReadLine();
复制代码
我们先来分析下你的程序运行流程
First  while ((str = streamreader.ReadLine()) != null)
这句是先读第一行,然后判断是否为空 然后赋值给  str
然后str = System.Text.RegularExpressions.Regex.Replace(str, "//s+", "");
这个进行替换 果断不会出错

然后我们再看你的第二种写法
while (streamreader.ReadLine() != null)
你这句已经把第一行读了 但是没有进行存储 也就是没有赋值给str
然后你接着 str = streamreader.ReadLine();
这个赋值给 str的已然是 第二行的内容了
这次循环内必然是不会错
但是紧接着下次循环 又 while (streamreader.ReadLine() != null)
又是读了第三行 没有进行存储
那么最后你的结果必然是只有第二行的内容了
如果你再多加几行
你会发现 你的结果会是 2 4 6 8 的才会显示出来

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

回复 使用道具 举报
黑骏马 发表于 2013-8-19 09:10
问题描述得好乱,都看不懂了,能不能把问题理清,代码也发全一点儿。

亲问题,重新描述了一遍
回复 使用道具 举报
问题描述得好乱,都看不懂了,能不能把问题理清,代码也发全一点儿。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马