【发布时间】:2014-01-03 17:16:35
【问题描述】:
在 *.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 库中是否存在错误?
【问题讨论】:
-
了解如何使用调试器。这不是免费的调试服务。
-
你尝试了什么?添加日志记录,例如查看您要替换的内容。你真的调用了用
{. {0}替换. {0}的方法吗?你能提供一个重现问题的小例子吗?所以:创建一个带有类似Foo. Bar的句子的.docx文件,并验证替换工作只调用document.ReplaceText(". B", ". B");。您需要隔离问题。 -
我通过了它,它看起来不错;它根本不会用一个替换两个空格。
-
是的,我正在调用该方法 - 我在其中设置了一个断点。在我的文档中,no-space-between-them 句子会按应有的方式进行更改,但 two-spaces-beween-them 句子不会。
标签: c# char docx spaces novacode-docx