Word开发工具Aspose.Words功能演示:使用C ++查找和替换Word文档中的文本

查找和替换是MS Word提供的一项有用功能,可以一次更新所有出现的特定文本。因此,不必手动查找和替换整个文档中的文本。

  • 查找和替换Word文档中的文本
  • 使用正则表达式查找和替换文本
  • 使用元字符替换文本
  • 在查找和替换操作过程中忽略文本

Aspose.Words for C ++是一个C ++库,用于创建新的和处理现有的Word文档。该API支持广泛的基本和高级Word自动化功能。

如果你还没有用过C ++版Aspose.Words可以点击这里下载最新版测试。

整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!


使用C ++查找和替换Word文档中的文本

以下是使用Aspose.Words for C ++在Word文档中查找和替换文本的步骤。

  • 使用Document类加载Word文档。
  • 使用Document-> get_Range()-> Replace(u“ sad”,u“ bad”,System :: MakeObject < FindReplaceOptions >(FindReplaceDirection :: Forward))方法替换所需的单词。
  • 使用Document-> Save(String)方法保存更新的Word文档。

下面的代码示例演示如何使用C ++在Word文档中查找“ sad”并将其替换为“ bad”。

// Load MS Word documentSystem::SharedPtrdoc = System::MakeObject(u"Document.doc");// Find and replace the textdoc->get_Range()->Replace(u"sad", u"bad", System::MakeObject(FindReplaceDirection::Forward));// Save the updated documentdoc->Save(u"updated.doc");

使用正则表达式查找和替换文本

可以定义正则表达式,以查找和替换遵循特定模式的单词。例如,您可以将单词“ sad”和“ mad”替换为“ bad”。以下是在Word文档中查找和替换与正则表达式匹配的单词的步骤。

  • 使用Document类加载Word文档。
  • 使用Regex类定义正则表达式。
  • 使用Document-> get_Range()-> Replace替换所需的单词(System :: MakeObject < System :: Text :: RegularExpressions :: Regex >(u” [s | m] ad”),u” bad”,选项)方法。
  • 使用Document-> Save(String)方法保存更新的Word文档。

下面的代码示例演示如何在C ++中使用正则表达式查找和替换文本。

// Load MS Word documentSystem::SharedPtrdoc = System::MakeObject(u"Document.doc");// Create find and replace optionsSystem::SharedPtroptions = System::MakeObject();// Find and replace the textdoc->get_Range()->Replace(System::MakeObject(u"[s|m]ad"), u"bad", options);// Save the updated documentdoc->Save(u"updated.doc");

使用元字符查找和替换文本

在某些情况下,您要替换的文本可能包含换行符,即换行符,段落换行符,分节符等。为了应对这种情况,Aspose.Words for C ++在搜索和替换字符串中支持以下元字符。

  • &p 换段
  • &b 用于分节符
  • &m 用于分页符
  • &l 手动换行

下面的代码示例演示如何使用Word文档中的元字符查找和替换文本。

// Load MS Word documentSystem::SharedPtrdoc = System::MakeObject(u"Document.doc");// Create find and replace optionsSystem::SharedPtroptions = System::MakeObject();// Find and replace the textdoc->get_Range()->Replace(u"This is Line 1&pThis is Line 2", u"This is replaced line", options);doc->get_Range()->Replace(u"This is Line 1&mThis is Line 2", u"Page break is replaced with new text.", options);// Save the updated documentdoc->Save(u"updated.doc");

在查找和替换操作过程中忽略文本

Aspose.Words for C ++还允许您在查找和替换操作期间忽略字段和修订中的文本。该FindReplaceOptions类,您可以指定选项来添加这个定制。FindReplaceOptions类提供以下方法来在不同情况下忽略文本:

  • set_IgnoreFields(bool) –忽略字段内的文本
  • set_IgnoreDeleted(bool) –忽略删除版本内的文本
  • set_IgnoreInserted(bool) –忽略插入修订中的文本

下面的代码示例演示如何在上述每种情况下忽略文本。

// Load MS Word documentSystem::SharedPtrdoc = System::MakeObject(u"Document.doc");// Create find and replace optionsSystem::SharedPtroptions = System::MakeObject();// Replace 'e' in document ignoring text inside fieldoptions->set_IgnoreFields(true);doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);// Replace 'e' in document ignoring deleted textoptions->set_IgnoreDeleted(true);doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);// Replace 'e' in document ignoring inserted textoptions->set_IgnoreInserted(true);doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);// Save the updated documentdoc->Save(u"updated.doc");


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月15日
下一篇 2021年1月15日

相关推荐

发表回复

登录后才能评论