2013-03-20 48 views
0

我有大约30按钮,每个按钮包含文本,然后一个数字,我只是需要把其他按钮的数量按钮名称最后一个字母

比如上例中button1.Name应该TEST3

这可能吗?

我知道,这样的LastIndexOf不起作用,这给了我想做的adchieve

button1 = "hello1"; //this varies in normal program 
button2 = "notimportant3"; //this varies in normal program 

button1.Name = "test"+button2.Name.LastIndexOf(1); 
+1

你试过了吗? – LukeHennerley 2013-03-20 12:25:58

+0

我不认为LastIndexOf在这里使用得当 – Yahya 2013-03-20 12:28:30

回答

1
button1.Name = "test" + button2.Name[button2.Name.Length-1]; 

或更好地利用的String.format的想法()格式化字符串

like

button1.Name = string.Format("test{0}", button2.Name[button2.Name.Length-1]); 
+0

出于好奇,为什么后或多或少同样的答案? :P – LukeHennerley 2013-03-20 12:31:07

+0

因为有时候人们可以同时发帖。 – 2013-03-20 12:32:24

+0

我完全喜欢这个网站:D谢谢!生病接受它一段时间 – Kapein 2013-03-20 12:33:13

1
button1.Name = string.Format("test{0}", button2.Name[button2.Name.Length - 1]); 
+0

谢谢!它正是它 – Kapein 2013-03-20 12:33:48

+0

@Kapein没有问题 - 你可以扩展这个,如果你想检查最后一个字符是一个数字,例如,如果这就是它必须是:) – LukeHennerley 2013-03-20 12:34:44

相关问题