Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C夏普替换Ms Word文档中出现的给定文本_C#_Text_Ms Word_Replace - Fatal编程技术网

C# 使用C夏普替换Ms Word文档中出现的给定文本

C# 使用C夏普替换Ms Word文档中出现的给定文本,c#,text,ms-word,replace,C#,Text,Ms Word,Replace,我正在用c夏普编码,我需要找到如何替换给定出现的文本 MS Word文档使用c夏普 我在网上找到了很多关于替换第一个事件和替换所有事件的例子,但在给定的事件中没有 我想要的示例如下: 你好世界你好测试你好 .. 你好测试你好 将来 你好世界你好测试你好 .. 树。。。测试你好 这是第四次出现“hello”被替换为“tree” 期待一个解决方案 谢谢这很有效。希望这就是你想要的: string s = "hello world hello test testing hello ..

我正在用c夏普编码,我需要找到如何替换给定出现的文本 MS Word文档使用c夏普

我在网上找到了很多关于替换第一个事件和替换所有事件的例子,但在给定的事件中没有

我想要的示例如下:

你好世界你好测试你好 .. 你好测试你好

将来

你好世界你好测试你好 .. 树。。。测试你好

这是第四次出现“hello”被替换为“tree”

期待一个解决方案


谢谢

这很有效。希望这就是你想要的:

        string s = "hello world hello test testing hello .. hello ... test hello";
        string[] value = { "hello" };
        string[] strList = s.Split(value,255,StringSplitOptions.None);
        string newStr = "";
        int replacePos = 4;
        for (int i = 0; i < strList.Length; i++)
        {
            if ((i != replacePos - 1) && (strList.Length != i + 1))
            {
                newStr += strList[i] + value[0];
            }
            else if (strList.Length != i + 1)
            {
                newStr += strList[i] + "tree";
            }
        }
string s=“你好世界你好测试你好..你好…测试你好”;
字符串[]值={“hello”};
string[]strList=s.Split(值255,StringSplitOptions.None);
字符串newStr=“”;
int-replacePos=4;
for(int i=0;i
这很有效。希望这就是你想要的:

        string s = "hello world hello test testing hello .. hello ... test hello";
        string[] value = { "hello" };
        string[] strList = s.Split(value,255,StringSplitOptions.None);
        string newStr = "";
        int replacePos = 4;
        for (int i = 0; i < strList.Length; i++)
        {
            if ((i != replacePos - 1) && (strList.Length != i + 1))
            {
                newStr += strList[i] + value[0];
            }
            else if (strList.Length != i + 1)
            {
                newStr += strList[i] + "tree";
            }
        }
string s=“你好世界你好测试你好..你好…测试你好”;
字符串[]值={“hello”};
string[]strList=s.Split(值255,StringSplitOptions.None);
字符串newStr=“”;
int-replacePos=4;
for(int i=0;i
试试这样的方法

static string ReplaceOccurrence(string input, string wordToReplace, string replaceWith, int occToReplace)
        {
            MatchCollection matches = Regex.Matches(input, string.Format("([\\w]*)", wordToReplace), RegexOptions.IgnoreCase);
            int occurrencesFound = 0;
            int captureIndex = 0;

            foreach (Match matchItem in matches)
            {
                if (matchItem.Value == wordToReplace)
                {
                    occurrencesFound++;
                    if (occurrencesFound == occToReplace)
                    {
                        captureIndex = matchItem.Index;
                        break;
                    }
                }
            }
            if (captureIndex > 0)
            {
                return string.Format("{0}{1}{2}", input.Substring(0, captureIndex), replaceWith, input.Substring(captureIndex + wordToReplace.Length));
            } else 
            {
                return input;
            }
        }

您必须使用System.Text.RegularExpressions放入
在顶部。

试试这样的方法

static string ReplaceOccurrence(string input, string wordToReplace, string replaceWith, int occToReplace)
        {
            MatchCollection matches = Regex.Matches(input, string.Format("([\\w]*)", wordToReplace), RegexOptions.IgnoreCase);
            int occurrencesFound = 0;
            int captureIndex = 0;

            foreach (Match matchItem in matches)
            {
                if (matchItem.Value == wordToReplace)
                {
                    occurrencesFound++;
                    if (occurrencesFound == occToReplace)
                    {
                        captureIndex = matchItem.Index;
                        break;
                    }
                }
            }
            if (captureIndex > 0)
            {
                return string.Format("{0}{1}{2}", input.Substring(0, captureIndex), replaceWith, input.Substring(captureIndex + wordToReplace.Length));
            } else 
            {
                return input;
            }
        }

您必须使用System.Text.RegularExpressions放入
在顶部。

您的意思是要编写在Word文档(如宏)中执行的代码,还是要在修改Word文档的服务器上执行代码?实际上,我想这样做。此链接提供如何替换一个和全部。所以我想这样做,这就是我所要求的。你是说你想写在Word文档中执行的代码(比如宏),还是想在服务器上执行修改Word文档的代码?实际上我想这样做。此链接提供如何替换一个和全部。所以我想这样做,这就是我所要求的,你可以这样使用这个<代码>字符串输出=替换发生(输入“hello”,“test”,4)其中输入是要搜索的字符串。您可以这样使用它<代码>字符串输出=替换发生(输入“hello”,“test”,4)其中输入是要搜索的字符串。