c#数字序列

c#数字序列,c#,string,sequence,C#,String,Sequence,我做这个代码是为了从2a3b到aabbb。当没有给出数字时,这也必须适用。比如aa2b=>aabb。 程序完全正常,但我的问题是,它占用了很多空间。我认为这是我的拆分,但如果输入为2a2b,我的数组将是这样的: 二, 无效的 无效的 A. 2. 无效的 无效的 b 有人知道我做错了什么吗?这是我的分手吗 static void Main(string[] args) { string test = ""; int intNumber

我做这个代码是为了从2a3b到aabbb。当没有给出数字时,这也必须适用。比如aa2b=>aabb。 程序完全正常,但我的问题是,它占用了很多空间。我认为这是我的拆分,但如果输入为2a2b,我的数组将是这样的:

二, 无效的 无效的 A. 2. 无效的 无效的 b

有人知道我做错了什么吗?这是我的分手吗

static void Main(string[] args)
        {
            string test = "";
            int intNumber = 1;

            string value = "2a2b";
            string[] array = new string[20];
            int count = 1;

            array = Regex.Split(value, "(\\d{0,2})");

            while (count < array.Length)
            {
                int num;
                if (array[count] != "")
                {
                    bool isNumeric = int.TryParse(array[count], out num);
                    if (!isNumeric)
                    {

                        test = test + string.fill(array[count], intNumber);
                        test = test + array[count];

                        Console.WriteLine(test);

                        intNumber = 1;
                    }
                    else
                    {
                        intNumber = num;

                    }  
                }
                count++;
            }
            Console.WriteLine("woord:" + test);


            Console.ReadLine();
static void Main(字符串[]args)
{
字符串测试=”;
整数=1;
字符串值=“2a2b”;
字符串[]数组=新字符串[20];
整数计数=1;
array=Regex.Split(值“(\\d{0,2})”;
while(计数<数组长度)
{
int-num;
if(数组[计数]!=“”)
{
bool isNumeric=int.TryParse(数组[count],out num);
如果(!isNumeric)
{
test=test+string.fill(数组[count],intNumber);
测试=测试+数组[计数];
控制台写入线(测试);
intNumber=1;
}
其他的
{
intNumber=num;
}  
}
计数++;
}
控制台写入线(“woord:+test”);
Console.ReadLine();

解决问题的一个更简单的方法是去掉regex,数组创建如下:

 char[] array = value.ToArray();
代码,由于
数组
和一些改进是一个字符数组(字符串数组的intead),有一些小的修正:


使用简单的
Regex.Replace如何

string input = "2a3bcccc";
string output = Regex.Replace(
         input, 
         @"(\d+)(\w)", 
         m => new String(m.Groups[2].Value[0],int.Parse(m.Groups[1].Value)));

结果:aabbbcccc

我通常会尽量避免使用正则表达式,除非有复杂的模式需要验证

以下是我对您问题的解决方案:

string k = Console.ReadLine();

string t = "";
int count = 0, next;

for (int i = 0; i < k.Length; i++)
{
    while (int.TryParse(k[i].ToString(), out next)) // Find the count of the next letter
    {
        count = count * 10 + next; // If count had a 2, and the next character is 3 (means we need to calculate 23), simply multiply the previous count by 10, and add the new digit
        i++; // Move to the next character
    }

    t += new String(k[i], count > 0 ? count : 1); // Add the new sequence of letters to our string
    count = 0; // Clear the current count
}

Console.WriteLine(t);
string k=Console.ReadLine();
字符串t=“”;
int count=0,下一步;
for(int i=0;i0?计数:1);//将新的字母序列添加到字符串中
count=0;//清除当前计数
}
控制台写入线(t);

您可以通过使用
StringBuilder
类来优化上述内容,但我认为,首先了解一般解决方案就足够了,而不是试图找到优化方法。

快速测试程序在不使用正则表达式的情况下工作得很好

const string value = "aa2b";
var result = "";

for (var i = 0; i < value.Length; i++)
{
     int num;
     if (Int32.TryParse(value.Substring(i, 1), out num))
     {
         for (var j = 0; j < num; j++)
         {
            result += value.Substring(i + 1, 1);
         }
            i++;
     }
     else
     {
         result += value.Substring(i, 1);
     }
}

textBox1.AppendText("woord:" + result);
const string value=“aa2b”;
var结果=”;
对于(变量i=0;i
按顺序逐个字符解析它。您将获得我的字符串类的边界(数字序列->第一个字符,字符序列->第一个数字),不包含方法
字符串。填充
我看不出您需要将数组初始化为
字符串[20]
如果您只是想在几行之后重新分配它。首先字符串是不可变的,所以不要使用加号(+)的串联我建议将StringBuilder类与Append方法一起使用。其次,string类中没有string.fill方法。它是自定义的,不是吗?您可以发布代码吗?是否将
999a
视为有效输入?您认为开始初始化
count=1;
正确吗?在我看来,它将跳过第一个元素
>“2”
Ahhh比使用“padleft”要好得多,而且更轻松。你可以只使用字符串构造函数,其中一个重载需要一个字符并多次重复。因此,如果你使用console.writeline(m),我会得到结果??谢谢你的回答!@user1765216 No use
console.writeline(output);
const string value = "aa2b";
var result = "";

for (var i = 0; i < value.Length; i++)
{
     int num;
     if (Int32.TryParse(value.Substring(i, 1), out num))
     {
         for (var j = 0; j < num; j++)
         {
            result += value.Substring(i + 1, 1);
         }
            i++;
     }
     else
     {
         result += value.Substring(i, 1);
     }
}

textBox1.AppendText("woord:" + result);