看板 C_Sharp 關於我們 聯絡資訊
噗~才剛po完問題,就爬到文了^^" 感謝1782篇,ithinkurdumb大大的貢獻.... 問題已解決,附上原始碼,給有需要的人參考~~~^^ string txt = txtPatn.Text.ToString(); Int32 int1 = txt.IndexOf("["); Int32 int2 = txt.IndexOf("]"); if(int1 != -1 && int2 != -1) { txt = txt.Remove(int1 + 1, int2-int1-1); txt = txt.Insert(int1 + 1, btn.Text.ToString()); txtPatn.Text = txt; } 呵,問題解決很開心哪^^ ※ 引述《colorsea (OH Yes! Babe~)》之銘言: : 各位大大好 : 我想做的功能是在textbox中會有一串文字 : 文字中會有部分是用中括號括起來的 : ex:It's a good [question]. : 按下按鈕後,會把按鈕中的文字取代原來中括號內的文字 : ex:按鈕為「answer」 : 按下後,textbox中的文字會變成:It's a good [answer]. : 看似簡單,可是讓身為新手的我搞了好久>"< : 請問怎麼找到中括號內的文字?取代的部分是用replace嗎? : 謝謝各位高手們^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.126.222
chhuang:Regex.Replace 更快.. 09/19 16:48
colorsea:如何用?請高手指點,已爬文看不太懂阿…有簡單版解說嗎 09/19 23:56
qweqweqweqwe:Regex r=new Regex(@"\[(\w*)\]"); 09/20 01:49
qweqweqweqwe:this.TextBox1.Text=r.Replace(this.TextBox1.Text, 09/20 01:49
qweqweqweqwe:"["+ this.Button1.Text+"]"); 09/20 01:49
tellmey:用regular expression真的有比較快嘛 ?@@ 09/20 09:29
MRjk:沒比較快 09/21 16:24
shinywong:寫起來比較快... 09/22 17:39