Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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中文本框中的部分文本#_C#_Ms Word - Fatal编程技术网

C# 如何替换c中文本框中的部分文本#

C# 如何替换c中文本框中的部分文本#,c#,ms-word,C#,Ms Word,我对在word自动化中使用c#有一个问题。 我的问题是我想替换文本框中的部分文本, 例如:“ABC 12345”并将12345替换为“123”,结果是“ABC 123” 但我不知道如何获取文本框中的部分文本,我使用 object firstshape = 1; string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text; 获取文本框中的原始文本,但我不知道如何获取部分文本的范围。 有没有办法在text

我对在word自动化中使用c#有一个问题。 我的问题是我想替换文本框中的部分文本, 例如:“ABC 12345”并将12345替换为“123”,结果是“ABC 123” 但我不知道如何获取文本框中的部分文本,我使用

object firstshape = 1;
string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;
获取文本框中的原始文本,但我不知道如何获取部分文本的范围。
有没有办法在textbox中获取任意范围的文本?提前谢谢大家

您可以像这样使用replace

    string Replace = "12345"; 
    string ReplaceWith = "123"
    text = text.Replace("12345","123")

要获取最后5个字符,请使用以下命令:

string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;
text = text.Substring(text.Length - 5, 5);
text = text.Replace(text, "123"); //to replace
使用Linq

string text = "ABC 12345";
string toReplace = text.Split().SkipWhile(x => x == "ABC").First();

为什么不使用像:
text=text.Replace(“12345”、“123”)