Why is this simple text replacement code not working?

1.2k views Asked by At

With text like this in a *.docx file:

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

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

...(IOW, two spaces between sentences in the first case, and no spaces in the second case) I want to force one and only one space between the sentences, so it ends up like so:

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

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

But this code:

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

...only works for the scrunched together sentences - The ones with two spaces between them fails to change. Why? Is there a bug in my code, or in the docx library?

3

There are 3 answers

2
CodeCaster On BEST ANSWER

I did what I said in my comment, downloaded DocX, created a Microsoft Word document and ran this code from a project referencing the DocX library:

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

And the Word file, prior containing:

Foo.Bar and Foo.  Bar

Afterwards contains:

Foo. Bar and Foo. Bar

So, works for me.

Edit: I ran your code on a file containing the first line from your question, and it works. Are you sure you're running this code and that you're looking at the correct file?

4
BlackBear On

You can do this with a regular expression instead:

using System.Text.RegularExpression;

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

The double negation is meant to match all whitespaces except newlines, normally included in the \s specifier.

1
user3364317 On

Try this code of docX.Replace() easy to change text from something text to another text.

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

            document.Save();
        } 
    }