-1

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?

Max
  • 12,622
  • 16
  • 73
  • 101
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Learn how to use a debugger. This is not a free debugging service. – Ondrej Tucny Jan 03 '14 at 17:24
  • What did you try? Add logging, for example to see what you are replacing with what. Do you actually call the method that replaces `. {0}` with `{. {0}`? Can you provide a small example that reproduces the problem? So: create a `.docx` file with a sentence like `Foo. Bar`, and to verify the replacement works only call `document.ReplaceText(". B", ". B");`. You need to isolate the issue. – CodeCaster Jan 03 '14 at 17:24
  • I stepped through it, and it looks fine; it simply doesn't replace the two spaces with one. – B. Clay Shannon-B. Crow Raven Jan 03 '14 at 17:27
  • Yes, I am calling the method - I put a breakpoint in it. And with my document, the no-space-between-them sentences get altered as they should, but the two-spaces-beween-them sentences do not. – B. Clay Shannon-B. Crow Raven Jan 03 '14 at 17:44

3 Answers3

2

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.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
1

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?

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Very odd; with a new document, it works for me now. The first one (the one that didn't work) contained text I pasted in to Word from Visual Studio, that's the only difference between the non-working and the working docx. – B. Clay Shannon-B. Crow Raven Jan 03 '14 at 17:48
  • Possibly it doesn't work when the text is split into multiple runs? Unzip your docx and look at the XML in document.xml – JasonPlutext Jan 03 '14 at 20:34
-2

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();
        } 
    }
  • 1
    This answer is more or less a duplicate of the already accepted answer posted by @CodeCaster. – kayess Dec 01 '15 at 13:08