C# 替换字符串中的SOH字符
我正在阅读一个小的MSWord文档,并将其内容存储在字符串中 此字符串中包含SOH特殊字符。在将它们写入新文本(.txt)文件之前,我想用占位符字符串替换它们,如“#placeholder 1”。请注意,我不想修改/编辑MSWord文档 我不确定string.Replace是否适合这个,或者我是否需要走不同的路线。它可能只是我用于SOH角色的参数C# 替换字符串中的SOH字符,c#,string,replace,C#,String,Replace,我正在阅读一个小的MSWord文档,并将其内容存储在字符串中 此字符串中包含SOH特殊字符。在将它们写入新文本(.txt)文件之前,我想用占位符字符串替换它们,如“#placeholder 1”。请注意,我不想修改/编辑MSWord文档 我不确定string.Replace是否适合这个,或者我是否需要走不同的路线。它可能只是我用于SOH角色的参数 建议?你所做的没有理由不起作用。下面是一个您可以测试的最小示例: 我认为您可能做错的另一件事是,没有存储调用Replace的结果。您可以使用以下函数读
建议?你所做的没有理由不起作用。下面是一个您可以测试的最小示例:
我认为您可能做错的另一件事是,没有存储调用
Replace
的结果。您可以使用以下函数读取Microsoft Word文档的内容(需要参考Microsoft.Office.Interop.Word
):
公共字符串ReadWordDoc(字符串路径)
{
//微软word应用程序对象
Microsoft.Office.Interop.Word.Application _objWord=null;
//microsoft word文档对象
Microsoft.Office.Interop.Word.Document _objDoc=null;
//obj缺少值(ms office)
对象_objMissing=System.Reflection.Missing.Value;
//用于保存文档内容的字符串生成器对象
StringBuilder_sb=新的StringBuilder();
尝试
{
//创建新的word应用程序对象
_objWord=新的Microsoft.Office.Interop.Word.Application();
//检查文件是否存在
如果(!File.Exists(Path))抛出(newfilenotfoundexception());
//文档的完整路径
对象_objDocPath=Path;
//只读标志
bool _objReadOnly=true;
//开放式word文档
_objDoc=_objWord.Documents.Open(
参考objDocPath,
参考号:objMissing,
_objReadOnly,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考号:objMissing,
参考文献(未列出);
//将整个内容读入StringBuilder obj
对于(int i=1;i您是如何表示SOH字符的?\x01是我目前使用的。我本以为它会起作用,但请尝试使用\u0001
。请参阅。您的搜索字符串中该字符后面是否有任何内容可能被误认为是转义序列的一部分?看起来每个SOH后面都有一个VT。最后一个SOH d没有与之配对的VT。每个SOH都有自己的线路不,我指的是您的搜索字符串。如果您正在搜索“\x01其他单词”
,那么转义字符串将被intereprated为\x01A
,这显然不是您想要的。如果您在问题中提供完整的代码示例可能是最好的。这基本上就是我所拥有的。我来看看,虽然它看起来可能是我正在阅读的word文档的问题。我用cop替换了它我在其他地方有,替换现在可以工作了。有趣的是,两个文件上的ascii输出是相同的…[1][11][13][1][13]我会接受这个答案,因为它肯定是正确的。感谢你的帮助,詹姆斯!感谢你的接受。我发现举一个简单的例子是有帮助的,因为它可以缩小问题的来源。在这种情况下,显然不是替换错了。
using System;
public class Test
{
public static void Main()
{
String s = "\u0001 This is a test \u0001";
s = s.Replace("\u0001","Yay!");
Console.WriteLine(s);
}
}
public string ReadWordDoc(string Path)
{
// microsot word app object
Microsoft.Office.Interop.Word.Application _objWord=null;
// microsoft word document object
Microsoft.Office.Interop.Word.Document _objDoc= null;
// obj missing value (ms office)
object _objMissing = System.Reflection.Missing.Value;
// string builder object to hold doc's content
StringBuilder _sb = new StringBuilder();
try
{
// create new word app object
_objWord= new Microsoft.Office.Interop.Word.Application();
// check if the file exists
if (!File.Exists(Path)) throw (new FileNotFoundException());
// full path to the document
object _objDocPath = Path;
// readonly flag
bool _objReadOnly = true;
// open word doc
_objDoc = _objWord.Documents.Open(
ref _objDocPath,
ref _objMissing,
_objReadOnly,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing,
ref _objMissing);
// read entire content into StringBuilder obj
for (int i = 1; i <= _objDoc.Paragraphs.Count; i++)
{
_sb.Append(_objDoc.Paragraphs[i].Range.Text.ToString());
_sb.Append("\r\n");
}
// return entire doc's content
return _sb.ToString();
}
catch { throw; }
finally
{
_sb = null;
if (_objDoc != null) { _objDoc.Close(); }
_objDoc = null;
if (_objWord != null) { _objWord.Quit(); }
_objWord = null;
}
}