有关提取单词文本和处理单元格中断字符的建议
寻求建议(也许是最佳实践)。
我们有一个 MS Word 文档 (Office 2007),我们正在从单元格中提取文本。
我们可以使用以下内容:
string text = wordTable.cell(tablerow.index, 1).Range.Text;
提取文本; 然而我们似乎得到了额外的字符尾随,例如 \r\a
。
现在我们可以添加以下内容:
.... wordTable.cell(tablerow.index, 1).Range.Text.Replace("\r\a,"");
但这似乎有点太懒了,而且几乎是浪费时间,很可能会导致以后出现问题。
我们还可以有一个方法来接收要清理的字符串:
private string cleanTextWordCellBreak(string wordTextToClean)
{
// Clean the text here
return cleanstring;
}
然后我们可以使用它:
cleanTextWordCellBreak(wordTable.cell(tablerow.index, 1).Range.Text;
);
这似乎更接近于处理该问题的更好方法。 你会怎么办?
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, for 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. What would you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将其分解为一个单独的方法,但使用替换实现,因为它是最简单的解决方案。 如果遇到问题,您可以随时更改实现(例如文本包含多个
\r\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:
我个人肯定会选择将其分解为一个单独的方法。 它有助于提高代码的可读性,并使将来需要时更容易更改。
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.
获取它的另一种方法是获取字符和字符的长度。 提取文本达到该长度。
<代码> <代码>
>
Another way of getting it would be get the length of Characters & extracting text upto that length.