问题描述
我试图使用Visual Studio和Windows形式相当长的一段写这本书的锻炼,现在在C#中,但是,我有最后几个步骤的麻烦和这本书有没有解决方案手册,我可以看看求助
下面是练习说:
- 读取文件中的
- 拆分文件中的行由行
- 按每一行上一摞
- POP每行出结果窗口
- 保存颠倒文件(句子应该扭转)。
下面是Windows窗体应该是什么样子的画面:
的
下面是示例.txt文件命名为社会正义,SampleText.txt:
So far I have created the form with the two rich textboxes and also created the two buttons "Open File" and "Split File." I also have read the .txt file in when I click the "Open File" button.
Here is what my form looks like based off of what I have done so far:
Here is my full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FileExercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void OpenFileButton_Click(object sender, EventArgs e)
{
StreamReader objstream = new StreamReader("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt");
richTextBox1.Text = objstream.ReadLine();
}
private void SplitFileButton_Click(object sender, EventArgs e)
{
}
}
}
So I am having trouble with steps 2-5 and was wondering if anyone could provide me an example of how to go about doing it based on what I have worked on already.
Thank you.
EDIT: Updated Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace FileExercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void OpenFileButton_Click(object sender, EventArgs e)
{
string TextFile = File.ReadAllText("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt", Encoding.UTF8);
richTextBox1.Text = TextFile;
}
private void SplitFileButton_Click(object sender, EventArgs e)
{
string SplitFile = File.ReadAllText("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt", Encoding.UTF8);
string[] SplitFileBySentence = Regex.Split(SplitFile, ".");
foreach (string Period in SplitFileBySentence)
{
richTextBox2.Text = Period;
}
}
}
}
You can use
string[] readText = File.ReadAllLines("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt")
to read all lines into string array.
Then, process the each line in the loop. I hope you can write the code to reverse the line.
这篇关于C#读/分割文本文件和倒车句子练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!