如何将文本文件分成一行一行的句子. [英] how to break a text file into line by line sentences.

查看:169
本文介绍了如何将文本文件分成一行一行的句子.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我将.docx/.doc文件转换为文本框中的文本文件时,我有一个word文件.我想分开句子.

我只想在一行中写一句话.

当有句号(.)时,光标会自动转到下一行.

我是编程新手.

请给我答复.

[评论删除]

Hi,
I have a word file.when i am converting .docx/.doc file to text file in a text box. i want separated sentences.

i want in one line only one sentence.

when there is full stop(.) then cursor automatically goes to next line.

i am new in programming.

please give me reply.

[Moved from Comment]

Microsoft.Office.Interop.Word.ApplicationClass wordObject = new Microsoft.Office.Interop.Word.ApplicationClass(); 
object File = txtfilepath.Text; //this is the path 
object nullobject = System.Reflection.Missing.Value; 
Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, 
   ref nullobject, ref nullobject, ref nullobject, ref nullobject, 
   ref nullobject, ref nullobject, ref nullobject, ref nullobject, 
   ref nullobject, ref nullobject, ref nullobject, ref nullobject); 
docs.ActiveWindow.Selection.WholeStory(); 
docs.ActiveWindow.Selection.Copy(); 
IDataObject data = Clipboard.GetDataObject(); 
txtshowfile.Text = data.GetData(DataFormats.Text).ToString(); 
string name = txtshowfile.Text; 
name = name.Replace('.','\n'); 
docs.Close(ref nullobject,ref nullobject,ref nullobject); 
wordobject.Quit(ref nullobject,ref nullobject,ref nullobject);

推荐答案

我没有遇到您的问题..,
请参阅以下代码,这可能对您有帮助

I am not getting your problem..,
see the below code this may helps you

string name = "Peter.Ronald.Garner.Simon.Liisha.Lori";
           name=name.Replace('.','\n');



顺便说一句,名称"包含您的数据.



BTW "name" contains your data.


如果您不关心句号中不用于结束句子的任何实例. (即先生,夫人,即...)
那么您应该只能够用句点-换行符组合替换文档中所有句点的实例.
If you don''t care about any instances of a period that is not used to end a sentence. (i.e. Mr., Mrs., i.e. ...)
then you should just be able to replace all instances of periods in your document with a period-newline combination.
doc = doc.replace( ".", "." + Environment.NewLine );


如果将此结果字符串写入文本文件,则每行应写一句话.

注意:
除非将Multiline属性设置为true,否则您将无法在文本框中正确看到结果.


If you write this resulting string to a textfile, you should have your one sentence per line done.

NOTE:
You won''t see the results properly in your Textbox unless you turn the Multiline property to true.


Marcus和Rajesh提出的建议是正确的方法.还可以考虑简化您的代码.例如,不需要使用剪贴板和定义所有可选参数.代码可能看起来像(可能包含很多错字):
What Marcus and Rajesh suggested is the correct way to go. Also consider simplifying your code. For example the usage of clipboard and defining all optional parameters is unnecessary. The code could look something like (may contain a lot of typos):
object File = txtfilepath.Text; //this is the path;
object nullobject = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document doc = wordobject.Documents.Open(ref File);
doc.ActiveWindow.Selection.WholeStory();
string allText = doc.ActiveWindow.Selection.Text;
doc.Close(ref nullobject, ref nullobject, ref nullobject);
wordobject.Quit();
allText = allText.Replace(".", "." + Environment.NewLine);


还要添加适当的try..catch块以处理可能的异常.


Also add proper try..catch blocks to handle possible exceptions.


这篇关于如何将文本文件分成一行一行的句子.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆