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

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

在本文中,将学习如何在C ++应用程序中查找和替换Word文档中的文本。当需要一次性替换多个文档中的特定文本时,这可能会很有用。

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

Aspose.Words for C ++最新下载(qun:761297826)icon-default.png?t=M85Bhttps://www.evget.com/product/4114


使用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 document
System::SharedPtrdoc = System::MakeObject(u"Document.doc");

// Find and replace the text
doc->get_Range()->Replace(u"sad", u"bad", System::MakeObject(FindReplaceDirection::Forward));

// Save the updated document 
doc->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 document
System::SharedPtrdoc = System::MakeObject(u"Document.doc");

// Create find and replace options
System::SharedPtroptions = System::MakeObject();

// Find and replace the text
doc->get_Range()->Replace(System::MakeObject(u"[s|m]ad"), u"bad", options);

// Save the updated document 
doc->Save(u"updated.doc");

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

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

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

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

// Load MS Word document
System::SharedPtrdoc = System::MakeObject(u"Document.doc");

// Create find and replace options
System::SharedPtroptions = System::MakeObject();

// Find and replace the text
doc->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 document 
doc->Save(u"updated.doc");

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

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

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

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

// Load MS Word document
System::SharedPtrdoc = System::MakeObject(u"Document.doc");

// Create find and replace options
System::SharedPtroptions = System::MakeObject();

// Replace 'e' in document ignoring text inside field
options->set_IgnoreFields(true);
doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);

// Replace 'e' in document ignoring deleted text
options->set_IgnoreDeleted(true);
doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);

// Replace 'e' in document ignoring inserted text
options->set_IgnoreInserted(true);
doc->get_Range()->Replace(System::MakeObject(u"e"), u"*", options);

// Save the updated document 
doc->Save(u"updated.doc");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值