【发布时间】:2016-01-06 21:41:34
【问题描述】:
我在 try catch 和 final 块中有一个函数我知道该函数将始终使用 finally 代码块但在这里它应该添加字符 我不想使用 while 循环
string increse_me ;
public void brut()
{
string[] small = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
string[] cap = new string [] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
string[] num = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
string[] spcl = new string[] { "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ">", "<", "+" };
if( textbox1.Text == increase_me)
{
messagebox.show("matched")
}
catch (exception ex)
{
}
finally
{
brut();
//here i have to call function again with increase letter as start from "a"it add next to it
}
}
现在如何制作一个函数,该函数第一次使用声明为 increase_me 的字符串,并在最终从第二个数组等失败时将其值增加到 26 max 位
【问题讨论】:
-
如果你想编写一个方法,在每次调用时将字符串移动到下一个排列,你必须检查当前字符串的最后一个字符(例如
"asd123B!"的"!"),并实施翻转("B+"变为"Ca")。这是可行的,但我希望你不要指望我们为你编写那个方法。 -
你知道这会因为无限递归导致堆栈溢出问题吗?
-
很难说你在问什么,但我认为你只是在寻找类似于“Generate all combinations for a list of strings”的东西
-
看过
String.Join方法吗? msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx -
如果目的是将每个数组 small、cap、num 和 spcl 中的字符串添加到
increse_me,可以只使用每个数组上的 Join 方法添加到 increase_me假设需要一个大宗交易。如果一个一个地重复执行每个字符串,那么可能需要一个循环,除非人们想编写一些非常丑陋的递归函数来传递不断缩小的数组以添加可能采用的另一条路线的值。
标签: c# .net combinations brute-force