c# - 为什么我的 c# 函数只能以这种方式工作?
问题描述
我正在尝试用 Open XML SDK 替换 docx 文件上的文本,它工作正常,所以它替换它,因为我写了“hi”和单词“hello”,将它们直接传递给 regexText.Replace,如下所示:
在职的
public static void SearchAndReplace(string document, string find, string replaced)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("hi");
docText = regexText.Replace("hello");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
}
但是当我尝试使用传递的变量调用函数时,代码不会用“hello”替换单词“hi”,但为什么呢?是什么改变了代码使其失败?(我的意思是代码正在编译并且它不会抛出任何异常,但它不会替换)
不工作
SearchAndReplace("template.docx", "hi", "hello");
并改变这一点,仅此而已:
Regex regexText = new Regex(find);
docText = regexText.Replace(docText, replace);
解决方案
推荐阅读
- swift - 在 Swift (Mac) 中运行工作流程
- amazon-web-services - aws-amplify PubSub.publish TypeError:无法读取未定义的属性“byteLength”
- javascript - 每次 Karma 测试后如何清理?
- microservices - Core Activiti 6 和 Activiti 7 相对于 Activiti 5 有哪些附加功能
- azure - ARM 模板部署 - 逻辑应用程序到事件网格主题和多个订阅调用更多逻辑应用程序
- c++ - 在 C++ 中将数组附加到 Json 值
- python-3.x - python3中sin(x)的奇怪散点图
- python - renderPM 找不到面部“Times-Roman”的 .pfb
- sql - Sqlite 查询以检索空字段中具有 0 的不同值
- c# - 找不到类型或命名空间名称“学生”