C#经典练习题

来源:互联网 发布:杰夫格林数据 编辑:程序博客网 时间:2024/06/03 08:17
文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,每行一个,标题有的长有的短,输出到
控制台的时候最多标题长度20,如果超过20,则截取长度17的子串并且最后添加“...”,加一个竖线后输出作者的名字。

            string[] lines = System.IO.File.ReadAllLines(@"c:\1.txt", Encoding.Default);
foreach (string line in lines)
{
string[] str1 = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string name;
if (str1[0].Length > 20)
{
name = str1[0].Substring(0, 17) + "...|";
}
else
{
name = str1[0] + "|";
}
string author = str1[1];
string ss = name + author;
Console.WriteLine( ss);
}
Console.ReadLine();
Console.ReadKey();


原创粉丝点击