views:

226

answers:

3

Looking for advice (perhaps best practice)

We have a MS Word document (office 2007) that we are extracting text from a cell.

We can use the following:

string text = wordTable.cell(tablerow.index, 1).Range.Text;

The text is extracted; however we seem to get extra characters trailing, example "\r\a".

Now we could add the following:

.... wordTable.cell(tablerow.index, 1).Range.Text.Replace("\r\a,"");

But this seems a little too lazy, and pretty much a waste of time that would most likely lead to problems down the road.

We could also have a method that receives the string to clean:

private string cleanTextWordCellBreak(string wordTextToClean)

{

// Clean the text here

return cleanstring;

}

then we could use it:

cleanTextWordCellBreak(wordTable.cell(tablerow.index, 1).Range.Text; );

This seems closer to a better way of handling the issue.

So I guess my question would be. What would you do?

Thanking all in advance!

A: 

I would definitely opt for breaking it out into a separate method personally. it helps with code readability and makes it a lot easier to change if needed in the future.

Blounty
A: 

I would break it out into a separate method but use the replace implementation since it's the simplest solution. You could always change the implementation later if you run into problem (like the text contains more than one \r\a and needs to be preserved)

So:

private string stripCellText(string text) { return text.Replace("\r\a", ""); }

string text = stripCellText(wordTable.cell(tablerow.index, 1).Range.Text);

Hans-Eric
A: 
shahkalpesh