简体   繁体   English

替换字符串中的SOH字符

[英]Replace SOH character in string

I am reading a small MSWord document and storing its contents in a string. 我正在阅读一个小的MSWord文档,并将其内容存储在字符串中。

There are SOH special characters included in this string. 此字符串中包含SOH特殊字符。 I would like to replace them with a placeholder string, like "#placeholder1" before they are written into a new text (.txt) file. 在将它们写入新文本(.txt)文件之前,我想用一个占位符字符串(例如“#placeholder1”)替换它们。 Note that I am not wanting to modify/edit the MSWord document 请注意,我不想修改/编辑MSWord文档

I'm not sure if string.Replace would suit this or if I need to go a different route. 我不确定string.Replace是否适合此情况,或者我是否需要走其他路线。 It may just be the parameter I am using for the SOH character. 它可能只是我用于SOH字符的参数。

Suggestions? 建议?

There's no reason why what you're doing shouldn't work. 没有理由为什么您的工作不起作用。 Here's a minimal example that you can test on ideone : 这是一个可以在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);
    }
}

The only other thing I can think you might be doing wrong, is not storing the result of your call to Replace . 我认为您可能做错的另一件事是,不存储对Replace的调用结果。

You can use the following function to read the content of Microsoft Word document (requires reference to Microsoft.Office.Interop.Word ): 您可以使用以下功能来读取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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM