簡體   English   中英

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

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

我在單詞自動化中使用c#時遇到問題。 我的問題是我想替換文本框中的部分文本,例如: “ ABC 12345”並用“ 123”替換12345,結果是“ ABC 123”,但是我不知道如何在文本框中獲取部分文本, 我用

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

獲取文本框中的原始文本,但我不知道如何獲取零件文本的范圍。 有什么解決方案可以在文本框中獲取任意范圍的文本? 提前非常感謝!!!

您可以像這樣使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM