替换字符串中的SOH字符

时间:2013-05-13 16:48:10

标签: c# string replace

我正在阅读一个小的MSWord文档,并将其内容存储在一个字符串中。

此字符串中包含SOH特殊字符。在将它们写入新文本(.txt)文件之前,我想用占位符字符串替换它们,例如“#placeholder1”。请注意,我不想修改/编辑MSWord文档

我不确定string.Replace是否适合这种情况,或者我是否需要采用不同的路线。它可能只是我用于SOH字符的参数。

建议?

2 个答案:

答案 0 :(得分:6)

你没有理由不去做。这是您可以在ideone上测试的最小示例:

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);
    }
}

我认为你可能做错的唯一另一件事就是不将你的通话结果存储到Replace

答案 1 :(得分:0)

您可以使用以下函数来阅读Microsoft Word文档的内容(需要引用Microsoft.Office.Interop.Word):

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;
    }
}
相关问题