-1

在 *.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 库中是否有错误?

4

3 回答 3

2

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

using System.Text.RegularExpression;

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

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

于 2014-01-03T17:24:15.183 回答
1

我按照我在评论中所说的做了,下载了 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

所以,对我有用。

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

于 2014-01-03T17:41:28.400 回答
-2

试试这个 docX.Replace() 代码,很容易将文本从某个文本更改为另一个文本。

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

            document.Save();
        } 
    }
于 2015-12-01T12:48:37.323 回答