迭代两个字符串之间的每个值

时间:2016-06-09 14:29:19

标签: .net vb.net loops

我的输入是两个字符串。我需要遍历这两个字符串之间的每个值(包括值本身)。

例如,从AACZ(例如AA AB AC .. AY AZ BA BB .. CY CZ

合法值应该是0 - 9和A-Z

01AD的迭代应该会导致01 02 .. 09 0A .. 0Z .. 10 11 12 .. 9Z AA AB AC AD

字符串长度应该是可变的,但是start和end总是具有相同的长度

AAA to ZZZ有效

目前我完全陷入困境。有什么建议吗?

这是我当前的方法(我将字符串lengt限制为6,并将值填充为“0”,例如AAA变为000AAA

    value1 = "A01"
    value2 = "A20"
    value1 = value1.PadLeft(6, "0")
    value2 = value2.PadLeft(6, "0")

    Dim start1 As Integer = Asc(value1.Substring(0, 1))
    Dim start2 As Integer = Asc(value1.Substring(1, 1))
    Dim start3 As Integer = Asc(value1.Substring(2, 1))
    Dim start4 As Integer = Asc(value1.Substring(3, 1))
    Dim start5 As Integer = Asc(value1.Substring(4, 1))
    Dim start6 As Integer = Asc(value1.Substring(5, 1))

    Dim stop1 As Integer = Asc(value2.Substring(0, 1))
    Dim stop2 As Integer = Asc(value2.Substring(1, 1))
    Dim stop3 As Integer = Asc(value2.Substring(2, 1))
    Dim stop4 As Integer = Asc(value2.Substring(3, 1))
    Dim stop5 As Integer = Asc(value2.Substring(4, 1))
    Dim stop6 As Integer = Asc(value2.Substring(5, 1))

    For p1 As Integer = start1 To stop1
      For p2 As Integer = start2 To stop2
        For p3 As Integer = start3 To stop3
          For p4 As Integer = start4 To stop4
            For p5 As Integer = start5 To stop5
              For p6 As Integer = start6 To stop6

                Dim result as string = Convert.ToChar(p1) &Convert.ToChar(p2) & Convert.ToChar(p3) & Convert.ToChar(p4) & Convert.ToChar(p5) & Convert.ToChar(p6)
                Console.WriteLine(result)

              Next
            Next
          Next
        Next
      Next
    Next

这对000001009999非常有效,但棘手的部分是000A01000A20。在第一次运行时start6 equals 1stop6 equals 0 for for for循环退出。

1 个答案:

答案 0 :(得分:2)

这有点棘手,但在思考之后,我想出一个想法,将这个数字视为基数为36的数字。我没有编写代码来将base-n数转换为另一个base-n,但我在StackOverflow中找到它。

我做了2个项目:

  1. 一个C#类库项目,我找到了我找到的base-n转换器。该类库将包含在第二个项目中。
  2. 用于测试问题的VB控制台项目。
  3. 首先将开始和停止数字从base-36转换为base-10。然后从开始编号到停止编号循环。在循环体中,将迭代从base-10转换为base-36。

    C#用于base-n转换器。项目名称是BaseN。不要忘记在第二个项目中包含这个项目。

    using System;
    
    namespace BaseN
    {
        public class BaseConverter
        {
            /// <summary>
            /// Converts the given decimal number to the numeral system with the
            /// specified radix (in the range [2, 36]).
            /// </summary>
            /// <param name="decimalNumber">The number to convert.</param>
            /// <param name="radix">The radix of the destination numeral system (in the range [2, 36]).</param>
            /// <returns></returns>
            public static string DecimalToArbitrarySystem(long decimalNumber, int radix)
            {
                const int BitsInLong = 64;
                const string Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
                if (radix < 2 || radix > Digits.Length)
                    throw new ArgumentException("The radix must be >= 2 and <= " + Digits.Length.ToString());
    
                if (decimalNumber == 0)
                    return "0";
    
                int index = BitsInLong - 1;
                long currentNumber = Math.Abs(decimalNumber);
                char[] charArray = new char[BitsInLong];
    
                while (currentNumber != 0)
                {
                    int remainder = (int)(currentNumber % radix);
                    charArray[index--] = Digits[remainder];
                    currentNumber = currentNumber / radix;
                }
    
                string result = new String(charArray, index + 1, BitsInLong - index - 1);
                if (decimalNumber < 0)
                {
                    result = "-" + result;
                }
    
                return result;
            }
    
            /// <summary>
            /// Converts the given number from the numeral system with the specified
            /// radix (in the range [2, 36]) to decimal numeral system.
            /// </summary>
            /// <param name="number">The arbitrary numeral system number to convert.</param>
            /// <param name="radix">The radix of the numeral system the given number
            /// is in (in the range [2, 36]).</param>
            /// <returns></returns>
            public static long ArbitraryToDecimalSystem(string number, int radix)
            {
                const string Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
                if (radix < 2 || radix > Digits.Length)
                    throw new ArgumentException("The radix must be >= 2 and <= " +
                        Digits.Length.ToString());
    
                if (String.IsNullOrEmpty(number))
                    return 0;
    
                // Make sure the arbitrary numeral system number is in upper case
                number = number.ToUpperInvariant();
    
                long result = 0;
                long multiplier = 1;
                for (int i = number.Length - 1; i >= 0; i--)
                {
                    char c = number[i];
                    if (i == 0 && c == '-')
                    {
                        // This is the negative sign symbol
                        result = -result;
                        break;
                    }
    
                    int digit = Digits.IndexOf(c);
                    if (digit == -1)
                        throw new ArgumentException(
                            "Invalid character in the arbitrary numeral system number",
                            "number");
    
                    result += digit * multiplier;
                    multiplier *= radix;
                }
    
                return result;
            }
        }
    }
    

    VB项目。项目名称为ConsoleApplication1。

    Imports BaseN
    
    Module Module1
    
        Sub Main()
    
            Dim value1 = "A01"
            Dim value2 = "A20"
    
            For i = BaseConverter.ArbitraryToDecimalSystem(value1, 36) To BaseConverter.ArbitraryToDecimalSystem(value2, 36)
                Console.WriteLine(BaseConverter.DecimalToArbitrarySystem(i, 36))
            Next
    
            Console.WriteLine("Press any key...")
            Console.ReadKey(True)
    
        End Sub
    
    End Module
    

    <强>结果。

    A01
    A02
    A03
    A04
    A05
    A06
    A07
    A08
    A09
    A0A
    A0B
    A0C
    A0D
    A0E
    A0F
    A0G
    A0H
    A0I
    A0J
    A0K
    A0L
    A0M
    A0N
    A0O
    A0P
    A0Q
    A0R
    A0S
    A0T
    A0U
    A0V
    A0W
    A0X
    A0Y
    A0Z
    A10
    A11
    A12
    A13
    A14
    A15
    A16
    A17
    A18
    A19
    A1A
    A1B
    A1C
    A1D
    A1E
    A1F
    A1G
    A1H
    A1I
    A1J
    A1K
    A1L
    A1M
    A1N
    A1O
    A1P
    A1Q
    A1R
    A1S
    A1T
    A1U
    A1V
    A1W
    A1X
    A1Y
    A1Z
    A20
    Press any key...
    

    我没有把领先的零点放在前面,我确定你在自己添加领先零点时遇到了很多麻烦。

    Pavel Vladov的参考文献:

      

    Quickest way to convert a base 10 number to any base in .NET?   http://www.pvladov.com/2012/07/arbitrary-to-decimal-numeral-system.html