【发布时间】:2020-10-02 16:06:39
【问题描述】:
我想做一个简单的字符串生成器。
用户输入字符串的“模板”。模板中的任何位置都可以有占位符。 然后他输入可能适合字符串中任何占位符的字符。 它应该如何工作:
输入:
a.b.
123
输出:
[
"a1b1", "a1b2", "a1b3",
"a2b1", "a2b2", "a2b3",
"a3b1", "a3b2", "a3b3"
]
我找到了一些我的旧python代码,但我完全不明白。
我将输入字符串拆分为字符串数组和点数组。 然后我尝试只增加点,每次都以正确的方式连接这两个数组。 但是我发现了一个新的麻烦。
string[] splitted = kt_NonCur.Split('.'); // array of constant strings
char[] nch = new char[splitted.Length - 1]; // array of new chars (generated)
char lgc = goodLetters.Last( ); // last good char
for( int i = 0; i < nch.Length - 1; i++ ) // set up all nch to first letter
nch[i] = goodLetters[0];
while( nch.Last( ) != lgc ) { // until last nch is set to last good char
outputData.Add($"{concatsplit(splitted, nch)}"); // concatsplit(s,n) concatenates two arrays into string
nch[0] = up(nch[0]); // up(char) gets next character from goodLetters. If there is no next, it returns first letter.
if( nch[0] == goodLetters[0] ) {
nch[1] = up(nch[1]);
if(nch[1] == goodLetters[0]){
nch[2] = up(nch[2]);
// .
// .
// .
}
}
}
问题是:我正面临两难境地。要么找到更好的方法,要么限制占位符的数量,这样代码阶梯就不会太长。当然我会添加一些代码来检查它是否是最后一个并停止为其他人执行代码,但我仍然必须制作
【问题讨论】:
-
要搜索的关键字是“排列”。 .Net 中没有此功能,但您应该能够找到一个库来帮助生成它们。
-
如果输入是:
a.b.和1会发生什么 -
添加到@JoelCoehoorn 评论,这里的确切术语将是“重复排列”(因为相同的替换字符可以在输出中多次出现。