0

このコードを作成して、2a3b から aabbb に移動しました。これは、番号が指定されていない場合にも適用する必要があります。aa2b => aabb のように。プログラムは完全に機能していますが、私の問題は、多くのスペースを必要とすることです。それは私の分割だと思いますが、入力が2a2bの場合、私の配列は次のようになります。

2 NULL NULL a 2 NULL NULL 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();
4

4 に答える 4

0

クイック テスト プログラムは、正規表現を使用せずに魅力的に機能します。

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);
于 2013-04-08T19:39:30.410 に答える
0

問題を解決するより簡単な方法は、正規表現を取り除くことです。配列の作成は次のようになります。

 char[] array = value.ToArray();

arraychar 配列 (string 配列ではなく) であることに起因するマイナーな修正といくつかの改善を含むコード:

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();
}
于 2013-04-08T19:27:13.593 に答える
0

検証する必要がある複雑なパターンがない限り、私は通常、正規表現を避けようとします。

これがあなたの問題に対する私の解決策です:

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最適化を見つけるよりも、最初に一般的な解決策を理解するだけで十分だと思います。

于 2013-04-08T19:35:47.027 に答える