为什么这个简单的文本替换代码不起作用?

时间:2014-01-03 17:16:35

标签: c# char docx spaces novacode-docx

在* .docx文件中使用这样的文字:

I scream.  You scream.  We all scream for ice cream.

I scream.You scream.We all scream for ice cream.

...(IOW,第一种情况下句子之间有两个空格,第二种情况下没有空格)我想强制句子之间只有一个空格,所以最终会这样:

I scream. You scream. We all scream for ice cream.

I scream. You scream. We all scream for ice cream.

但是这段代码:

// 65..90 are A..Z; 97..122 are a..z
const int firstCapPos = 65;
const int lastCapPos = 90;
const int firstLowerPos = 97;
const int lastLowerPos = 122;

    . . .

// This will change sentences like this: "I scream.You scream.We all scream of ice cream." ...to this: "I scream. You scream. We all scream of ice cream."
private void SpacifySardinizedLetters(string filename)
{
    using (DocX document = DocX.Load(filename))
    {
        for (int i = firstCapPos; i <= lastCapPos; i++)
        {
            char c = (char)i;
            string originalStr = string.Format(".{0}", c);
            string newStr = string.Format(". {0}", c);
            document.ReplaceText(originalStr, newStr);
        }
        for (int i = firstLowerPos; i <= lastLowerPos; i++)
        {
            char c = (char)i;
            string originalStr = string.Format(".{0}", c);
            string newStr = string.Format(". {0}", c);
            document.ReplaceText(originalStr, newStr);
        }
        document.Save();
    }
}

// This will change sentences like this: "I scream.  You scream.  We all scream of ice cream." ...to this: "I scream. You scream. We all scream of ice cream."
private void SnuggifyLooseyGooseySentenceEndings(string filename)
{
    using (DocX document = DocX.Load(filename))
    {
        for (int i = firstCapPos; i <= lastCapPos; i++)
        {
            char c = (char)i;
            string originalStr = string.Format(".  {0}", c);
            string newStr = string.Format(". {0}", c);
            document.ReplaceText(originalStr, newStr);
        }
        for (int i = firstLowerPos; i <= lastLowerPos; i++)
        {
            char c = (char)i;
            string originalStr = string.Format(".  {0}", c);
            string newStr = string.Format(". {0}", c);
            document.ReplaceText(originalStr, newStr);
        }
        document.Save();
    }
}

...仅适用于拼凑在一起的句子 - 它们之间有两个空格的句子无法改变。为什么?我的代码或docx库中是否有错误?

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式来执行此操作:

using System.Text.RegularExpression;

string text = readFromDocx();
string newText = Regex.Replace( text, @"\.[^\S\n]*(\w)",
    m => string.Format( ". {0}", m.Groups[ 1 ] ) )

双重否定用于匹配除换行符之外的所有空格,通常包含在\s说明符中。

答案 1 :(得分:1)

我完成了我在comment中所说的,下载了DocX,创建了一个Microsoft Word文档,并从引用DocX库的项目中运行了此代码:

// Contains "Foo.Bar and Foo.  Bar"
string filename = "TestWordDocument.docx";

using (DocX document = DocX.Load(filename))
{
    document.ReplaceText(".B", ". B");
    document.ReplaceText(".  B", ". B");
    document.Save();
})

以前的Word文件包含:

Foo.Bar and Foo.  Bar

之后包含:

Foo. Bar and Foo. Bar

所以,对我有用。

编辑:我在包含您问题中第一行的文件上运行了代码,但它确实有效。您确定要运行此代码并且正在查看正确的文件吗?

答案 2 :(得分:-2)

尝试使用docX.Replace()代码可以轻松地将文本从文本更改为另一个文本。

static void Replace(string filename, string a, string b)
    {
        using (DocX document = DocX.Load(filename))
        {
            document.ReplaceText(a, b);

            document.Save();
        } 
    }