使用 C Sharp 替换 Ms Word 文档中给定的文本

发布于 2024-11-07 00:20:32 字数 345 浏览 3 评论 0原文

我正在用 c Sharp 编码,我需要找到如何替换 a 中给定文本的出现 使用 c Sharp 的 MS-Word 文档。

我在网上找到了很多关于替换第一次出现和替换所有出现的示例,但没有在给定的出现中出现。

我想要的一个例子如下:

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

你好

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

这是第四次出现的 'hello' 被替换为 'tree'。

期待解决方案...

谢谢

I m coding in c sharp and I need to find how to replace given occurrence of a text in a
MS-Word Document using c sharp.

I found so many examples on the web regarding replacing the first occurrence and replacing all occurrences but none on a given occurrence.

An example of what I want is as follows:

hello world hello test testing hello
.. hello ... test hello

to be

hello world hello test testing hello
.. tree ... test hello

That is the 4th occurance of 'hello' to be replaced with 'tree'.

Looking forward for a solution...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2024-11-14 00:20:32

尝试这样的事情...

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;
            }
        }

您必须在顶部放置 using System.Text.RegularExpressions;

Try something like this...

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;
            }
        }

You will have to put a using System.Text.RegularExpressions; at the top.

旧伤还要旧人安 2024-11-14 00:20:32

这有效。希望这就是您正在寻找的:

        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";
            }
        }

This works. Hope this is what you are looking for:

        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";
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文