c#数字序列

本文关键字:字序 数字 | 更新日期: 2023-09-27 18:08:01

我已经使这个代码从2a3b到aabbb。这也适用于没有给出数字的情况。比如aa2b => aabb。程序是完全工作,但我的问题是,它占用了很多空间。我想这是我的分割但是我的数组是这样的如果输入是2a2b:

2零零一个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();

c#数字序列

使用简单的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)));

result: aabbbcccc

解决问题的一个更简单的方法是摆脱regex,数组创建将像:

 char[] array = value.ToArray();

代码,由于array的小修正和一些改进是一个字符数组(而不是字符串数组):

static void Main(string[] args)
{
    string test = "";
    int intNumber = 1;
    string value = "2a2b";
    foreach (char c in value.ToArray())
    {
        int num;
        bool isNumeric = int.TryParse(c.ToString(), out num);
        if (!isNumeric)
        {
            test = test + new string(c, intNumber);
            Console.WriteLine(test);
            intNumber = 1;
        }
        else
        {
            intNumber = num;
        }
    }
    Console.WriteLine("woord:" + test);
    Console.ReadLine();
}

不使用正则表达式的快速测试程序就像一个魅力。

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);

我通常会尽量避免使用Regex,除非有需要验证的复杂模式。

这是我对你的问题的解决方案:

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);

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