我需要替换Word 文档中所有以$开头的单词
样本:
$Address
$Lastname etc.
现在一开始我必须创建一个包含所有以 $ 开头的单词的列表,然后我替换所有单词
$Lastname -> Waning etc
如何在 spiredoc 中创建所有以$开头的单词的列表?
您可以使用正则表达式和FillAllPattern()
方法查找以 $ 开头的单词并在 TextSelection 集合中返回结果。
Regex regex = new Regex(@"\$\w+\b");
TextSelection[] selections = document.FindAllPattern(regex);
要将与特定正则表达式匹配的字符串替换为新字符串,请使用Document.Replace(System.Text.RegularExpressions.Regex Pattern, string replace)
方法。
阅读文件。使用 Split() 拆分单词并将结果保存在列表中
string s = "word file text";
List<string> words = s.Split(' ');
和List中的控制项
List<string> result = new List<string>();
foreach(string item in words)
{
if (item .StartsWith("$"))
{
result.Add(item);
}
}
结果返回包含 $ 的字符串