C# 比较docx中的字符串和行

C# 比较docx中的字符串和行,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我是c#新手,我正在开发一个关于分析工作简历的程序,我对字符串比较有一些问题 当我将字符串与stringReader中的行进行比较时,即使字符串相同,我的比较输出也是false。我真的对此感到困惑 我试过Regex.Ismatch和String.Equals。我尝试用Regex.replace()替换第行中的特殊字符,但没有一个给出正确的输出 以下信息可能会有所帮助: 当我使用 Console.WriteLine(Regex.Escape(line.ToString())); 我得到 在屏

我是c#新手,我正在开发一个关于分析工作简历的程序,我对字符串比较有一些问题

当我将字符串与stringReader中的行进行比较时,即使字符串相同,我的比较输出也是false。我真的对此感到困惑

我试过Regex.Ismatch和String.Equals。我尝试用Regex.replace()替换第行中的特殊字符,但没有一个给出正确的输出

以下信息可能会有所帮助:

当我使用

 Console.WriteLine(Regex.Escape(line.ToString())); 
我得到

在屏幕上

当我打电话时

Console.WriteLine(Regex.IsMatch("Job Description - Ensure Alignment to Selected Title", @"Job\ Description\ -\ Ensure\ Alignment\ to\ Selected\ Title"));
我得到的结果是真的

但是

给我假消息

有些棘手的事情是 当我打电话时

Console.WriteLine(line.ToString().Length);
输出为53

我认为

Job\ Description\ -\ Ensure\ Alignment\ to\ Selected\ Title
应该是52岁

以下是我的部分代码:

// this part is how I change docx file to a string
StringBuilder text = new StringBuilder();
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"D:\testfile1.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                text.Append(docs.Paragraphs[i + 1].Range.Text.ToString());

            }

// this part is the comparison
using (StringReader reader = new StringReader(text.ToString()))
            {
                string line = string.Empty;
                line = reader.ReadLine();
                if(string.Equals("Job Description - Ensure Alignment to Selected Title", line.ToString())){
                 Console.WriteLine("correct");}
}

//这部分是如何将docx文件更改为字符串的
StringBuilder text=新的StringBuilder();
Microsoft.Office.Interop.Word.Application Word=新的Microsoft.Office.Interop.Word.Application();
对象未命中=System.Reflection.Missing.Value;
对象路径=@“D:\testfile1.docx”;
对象只读=真;
Microsoft.Office.Interop.Word.Document docs=Word.Documents.Open(参考路径,参考未命中,参考只读,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中,参考未命中);
对于(int i=0;i
尝试以下操作:将
text
的内容输出到file(
file.writealText(@“c:\path”,text.ToString()
)中。现在打开该文件并粘贴到单词doc中“same”正下方的行中行。直接比较它们。如果它们在纯文本查看器中进行精确比较,请使用某种十六进制编辑器查看实际字节(甚至“-”可能不同)。如果您在windows上,只需使用powershell的
格式十六进制
,Word段落将具有“段落标记”在它的结尾。这将是一个ANSI 13字符。在C中,这可以用
“\r”
,“
\n”
char(13)
,我记得。我猜这是你计算长度时的第53个字符。。。
Job\ Description\ -\ Ensure\ Alignment\ to\ Selected\ Title
// this part is how I change docx file to a string
StringBuilder text = new StringBuilder();
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"D:\testfile1.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                text.Append(docs.Paragraphs[i + 1].Range.Text.ToString());

            }

// this part is the comparison
using (StringReader reader = new StringReader(text.ToString()))
            {
                string line = string.Empty;
                line = reader.ReadLine();
                if(string.Equals("Job Description - Ensure Alignment to Selected Title", line.ToString())){
                 Console.WriteLine("correct");}
}