繁体   English   中英

仅当它不是另一个字符串的一部分时,如何替换 Aspose.Words (C#) 中的字符串?

[英]How to replace a string in Aspose.Words (C#) only if it is not part of another string?

我想使用这行代码将所有出现的“2009”替换为“2000”,但我不想替换“2009”是其他内容的一部分。 例如“2009 年”不应该被替换为“2000 年”,那些应该保留。

doc.Range.Replace("2009", "2000", options);
// "It was year 2009, and they already have collected 2009 votes."
// I want the this to change to:
// "It was year 2009, and they already have collected 2000 votes."

有没有办法使用 21.9.0 版来做到这一点。

我认为您所描述的唯一解决方案是用任意占位符替换“2009 年”,并在替换其他实例后将其放回原处。 像这样的东西会做你想做的事,同时很容易定制:

doc.Range.Replace("year 2009", "mySillyPlaceholder");
doc.Range.Replace("2009", "2000");
doc.Range.Replace("mySillyPlaceholder", "year 2009");

但是,理想情况下,您应该在文件生成之前更改一个数字变量,因为这是确保它在正确的上下文中完成的最佳方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM