Click here to Skip to main content
12,943,528 members (50,177 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 6 Jan 2014

Insert Spaces Between CrammedTogetherWords in .docx files using C# and the DocX Library

, 7 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Yet another docx tip - insert spaces between lowercase and uppercase chars


Sometimes you may end up with a .docx file that has words jammed and crammed together like this:

CuriousGeorgeWashingtonIrvingStone theCrowsNest ofRobberBaron vonMunchabaloneyB. WhiterShade ofPaleRiders

You want it, of course, to be like this instead:

Curious George Washington Irving Stone the Crows Nest of Robber Baron von Munchabaloney B. Whiter Shade of Pale Riders

Automatically replacing all the lowercase-followed-by-uppercase-with-no-space-between-them instances is easy; I'll show you how. First, though:


Follow these steps to prepare for the code to follow:

  1. Download the DocX DLL library from here
  2. In your Visual Studio project, right-click References, select "Add Reference..." and add docx.dll to the project from wherever you saved it.
  3. Add this to your using section:
    using Novacode;


Add these consts to the top of your class:

const int firstCapPos = 65;
const int lastCapPos = 90;
const int firstLowerPos = 97;
const int lastLowerPos = 122;

Add code like this to call the helper function that will insert spaces between lowercase chars and uppercase chars:

string filename = string.Empty;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
    filename = openFileDialog1.FileName;
    MessageBox.Show("No file selected - exiting");

Now add the helper method:

private void InsertSpaceBetweenLowercaseAndUppercaseChars(string filename)
    using (DocX document = DocX.Load(filename))
        for (int i = firstLowerPos; i <= lastLowerPos; i++)
            char lowerChar = (char)i;
            for (int j = firstCapPos; j <= lastCapPos; j++)
                char upperChar = (char)j;
                string originalStr = string.Format("{0}{1}", lowerChar, upperChar);
                string newStr = string.Format("{0} {1}", lowerChar, upperChar);
                document.ReplaceText(originalStr, newStr);

Of course, you would only want to use this on "normal" English (prose, etc.) If you used it on code, you would end up changing "InsertSpaceBetweenLowercaseAndUppercaseChars" to "Insert Space Between Lowercase And Uppercase Chars", etc. - probably not what you want.

Everything Works in Theory

If this tip benefits you, be sure to try this one the next time you have the hiccups (or, even worse, the hiccoughs), as it is a surefire* cure for that malady: Run around the house three times without thinking of the word tiger.

* It has never been proven to be ineffective!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 7 Jan 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid