Insert spaces between words
by clayshannon
HTML
<h2>Theory</h2>
<p>Sometimes you may end up with a .docx file that has words jammed and crammed together like this:</p>
<p><em>CuriousGeorgeWashingtonIrvingStone theCrowsNest ofRobberBaron vonMunchabaloneyB. WhiterShade ofPaleRiders</em></p>
<p>You want it, of course, to be like this instead:</p>
<p><em>Curious George Washington Irving Stone the Crows Nest of Robber Baron von Munchabaloney B. Whiter Shade of Pale Riders</em></p>
<p>Automatically replacing all the lowercase-followed-by-uppercase-with-no-space-between-them instances is easy; I'll show you how. First, though:</p>
<h2>Regression</h2>
<p>Follow these steps to prepare for the code to follow:</p>
<ol>
<li>Download the DocX DLL library from <a href="https://docx.codeplex.com/">here</a></li>
<li>In your Visual Studio project, right-click References, select "Add Reference..." and add docx.dll to the project from wherever you saved it.</li>
<li>Add this to your using section: <pre>using Novacode;</pre></li>
</ol>
<h2>Practice</h2>
<p>Add code like this to call the helper function that will insert spaces between lowercase chars and uppercase chars:</p>
<pre>
string filename = string.Empty;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
filename = openFileDialog1.FileName;
}
else
{
MessageBox.Show("No file selected - exiting");
return;
}
InsertSpaceBetweenLowercaseAndUppercaseChars(filename);
</pre>
<p>Now add the helper method:</p>
<pre>
private void InsertSpaceBetweenLowercaseAndUppercaseChars(string filename)
{
using (DocX document = DocX.Load(filename))
{
for (int i = firstLowerPos; i <= lastLowerPos; i++)
{
for (int j = firstCapPos; j <= lastCapPos; j++)
{
...