如何在复制到文本文件之前删除特殊字符?

时间:2013-03-29 09:09:18

标签: c#

以下是用于从Document读取数据并移植到文本文件中的代码,

但是,在写入文本文件之前,我想删除或忽略文档中存在的特殊字符。特殊字符表示箭头,项目符号,复制写入符号等。,当涉及到文本文件时,它显示一些随机字符。所以,我想在写入文本文件之前删除或忽略那些字符或符号。

object file;

file = filepathtb.Text;

object Target = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Temp_str.txt";
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();

object Unknown = Type.Missing;
newApp.Documents.Open(ref file, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

// if(newApp.ActiveDocument.Content.Characters = a

newApp.ActiveDocument.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown,
    ref Unknown, ref Unknown, ref Unknown,
    ref Unknown, ref Unknown, ref Unknown,
    ref Unknown, ref Unknown, ref Unknown,
    ref Unknown, ref Unknown);

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

string myText = "sample text...";
string formattedText = String.Empty;

foreach(char c in myText)
{
    if(Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c) || Char.IsPunctuation(c))
        formattedText += c;
}