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

时间:2013-02-19 06:36:12

标签: c# ms-word

我在word自动化中使用c#时遇到问题。 我的问题是我想替换文本框中的部分文本, 例如:“ABC 12345”并用“123”替换12345,因此,“ABC 123” 但我不知道如何在文本框中获取部分文本,我使用

object firstshape = 1;
string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;

在文本框中获取原始文本,但我不知道如何获取部分文本的范围。 是否有任何解决方案可以在文本框中获取任何范围的文本?非常感谢提前!!!

3 个答案:

答案 0 :(得分:1)

你可以像这样使用

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

答案 1 :(得分:0)

要获得最后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

答案 2 :(得分:0)

使用Linq

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