简体   繁体   English

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

[英]how do I replace part of text in textbox in c#

I have a problem about using c# in word automation. 我在单词自动化中使用c#时遇到问题。 My problem is I want to replace part of text in a textbox, ex: "ABC 12345" and replace 12345 by "123" , as a result, "ABC 123" But I don't know how to get part of text in textbox, I use 我的问题是我想替换文本框中的部分文本,例如: “ ABC 12345”并用“ 123”替换12345,结果是“ ABC 123”,但是我不知道如何在文本框中获取部分文本, 我用

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

to get the original text in textbox,but i don't know how to get the range of part text. 获取文本框中的原始文本,但我不知道如何获取零件文本的范围。 Is there any solution to get any range of text in textbox? 有什么解决方案可以在文本框中获取任意范围的文本? thanks a lot in advance!!! 提前非常感谢!!!

You can use replace like this 您可以像这样使用replace

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

To get last 5 characters use this: 要获取最后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

Use Linq 使用Linq

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM