通过键而不是一个键递增数组中的字符

问题描述 投票:-1回答:1

我的英语不太好,对此标题感到抱歉。

我想制作一个简单的字符串生成器。

用户输入字符串的“模板”。模板可以在其中的任何位置使用通配符。然后,他输入可能适合字符串中任何通配符的字符。它应该如何工作:

输入:

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]);
//                          .
//                              .
//                                  .
        }
    }
}

而且问题是:我正面临一种疾病。要么找到更好的方法,要么限制通配符的数量,以使代码阶梯不会太长。当然,我会添加一些代码来检查它是否是最后一个,并停止为其他代码执行代码,但是我仍然必须使

c# string generator
1个回答
0
投票

您可以这样看待您的问题:如果您的输入字符串中有P个占位符,并且替换字符的数量为R,则要构造每一步所需的每个可能的输出字符串,P个数字为[0 ... R-1 ](然后可以用作替换字符列表的索引)。好吧,这是一个以R为底的P位整数的定义。

因此,让我们编写一个表示此类整数的帮助器类:

class NDigitNumber
{
    int[] _digits;
    int _base;

    // construct an integer with the specified numer of digits in the specified base
    public NDigitNumber(int digits, int @base)
    {
        _digits = new int[digits];
        _base = @base;
    }

    // get the digit at the specified position
    public int this[int index] => _digits[index];

    // increment the number, returns false on overflow
    public bool Increment()
    {
        for (var pos = 0; pos < _digits.Length; pos++)
        {
            if (++_digits[pos] < _base)
                break;
            if (pos == _digits.Length-1)
                return false;
            for (var i = 0; i <= pos; i++)
                _digits[i] = 0;
        }
        return true;
    }
}

然后,我们只需要遍历所有可能的整数以获得所需的输出:

var input = "a.b.";
var placeholder = '.';
var replacements = new[] { '1', '2', '3' };

// determine positions of placeholder in string
var placeholderPositions = new List<int>();
for (var i = 0; i < input.Length; i++)
{
    if (input[i] == placeholder)
        placeholderPositions.Add(i);
}

// iterate over all possible integers with
// placeholderPositions.Count digits
// in base replacements.Length
var number = new NDigitNumber(placeholderPositions.Count, replacements.Length);
do
{
    var result = new StringBuilder(input);
    for (var i = 0; i < placeholderPositions.Count; i++)
        result[placeholderPositions[i]] = replacements[number[i]];
    Console.WriteLine(result.ToString());
} while(number.Increment());
© www.soinside.com 2019 - 2024. All rights reserved.