Increment a string from numbers 0-9 to lowercase a-z to uppercase A-Z in C#

前端 未结 3 1372
自闭症患者
自闭症患者 2020-12-22 03:12

I want to be able to have a string 6 characters long starting with \'000000\'. Then I want to increment it by one \'000001\' when I hit 9 I want to go to \'00000a\' when I g

相关标签:
3条回答
  • 2020-12-22 03:49

    Here's another approach...it allows you to pass in a "starting revision" ("000000" in the example). I originally wrote it in VB.Net, a very long time ago, in response to a question with very specific requirements...so the below may not be the most efficient way of doing things.

    public partial class Form1 : Form
    {
    
        private Revision rev;
    
        public Form1()
        {
            InitializeComponent();
            Reset();
        }
    
        private void Reset()
        {
            rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "000000");
            label1.Text = rev.CurrentRevision;
        }
    
        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
    
        private void btnNext_Click(object sender, EventArgs e)
        {
            rev.NextRevision();
            if (rev.CurrentRevision.Length == 7)
            {
                MessageBox.Show("Sequence Complete");
                Reset();
            }
            else
            {
                label1.Text = rev.CurrentRevision;
            }
        }
    
    }
    
    public class Revision
    {
    
        private string chars;
        private char[] values;
    
        private System.Text.StringBuilder curRevision;
    
        public Revision()
        {
            this.DefaultRevision();
        }
    
        public Revision(string validChars)
        {
            if (validChars.Length > 0)
            {
                chars = validChars;
                values = validChars.ToCharArray();
                curRevision = new System.Text.StringBuilder(values[0]);
            }
            else
            {
                this.DefaultRevision();
            }
        }
    
        public Revision(string validChars, string startingRevision)
            : this(validChars)
        {
            curRevision = new System.Text.StringBuilder(startingRevision);
            int i = 0;
            for (i = 0; i <= curRevision.Length - 1; i++)
            {
                if (Array.IndexOf(values, curRevision[i]) == -1)
                {
                    curRevision = new System.Text.StringBuilder(values[0]);
                    break;
                }
            }
        }
    
        private void DefaultRevision()
        {
            chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            values = chars.ToCharArray();
            curRevision = new System.Text.StringBuilder(values[0]);
        }
    
        public string ValidChars
        {
            get { return chars; }
        }
    
        public string CurrentRevision
        {
            get { return curRevision.ToString(); }
        }
    
        public string NextRevision(int numRevisions = 1)
        {
            bool forward = (numRevisions > 0);
            numRevisions = Math.Abs(numRevisions);
            int i = 0;
            for (i = 1; i <= numRevisions; i++)
            {
                if (forward)
                {
                    this.Increment();
                }
                else
                {
                    this.Decrement();
                }
            }
            return this.CurrentRevision;
        }
    
        private void Increment()
        {
            char curChar = curRevision[curRevision.Length - 1];
            int index = Array.IndexOf(values, curChar);
            if (index < (chars.Length - 1))
            {
                index = index + 1;
                curRevision[curRevision.Length - 1] = values[index];
            }
            else
            {
                curRevision[curRevision.Length - 1] = values[0];
                int i = 0;
                int startPosition = curRevision.Length - 2;
                for (i = startPosition; i >= 0; i += -1)
                {
                    curChar = curRevision[i];
                    index = Array.IndexOf(values, curChar);
                    if (index < (values.Length - 1))
                    {
                        index = index + 1;
                        curRevision[i] = values[index];
                        return;
                    }
                    else
                    {
                        curRevision[i] = values[0];
                    }
                }
                curRevision.Insert(0, values[0]);
            }
        }
    
        private void Decrement()
        {
            char curChar = curRevision[curRevision.Length - 1];
            int index = Array.IndexOf(values, curChar);
            if (index > 0)
            {
                index = index - 1;
                curRevision[curRevision.Length - 1] = values[index];
            }
            else
            {
                curRevision[curRevision.Length - 1] = values[values.Length - 1];
                int i = 0;
                int startPosition = curRevision.Length - 2;
                for (i = startPosition; i >= 0; i += -1)
                {
                    curChar = curRevision[i];
                    index = Array.IndexOf(values, curChar);
                    if (index > 0)
                    {
                        index = index - 1;
                        curRevision[i] = values[index];
                        return;
                    }
                    else
                    {
                        curRevision[i] = values[values.Length - 1];
                    }
                }
                curRevision.Remove(0, 1);
                if (curRevision.Length == 0)
                {
                    curRevision.Insert(0, values[0]);
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-22 03:50

    This uses the IntToString supporting arbitrary bases from the question Quickest way to convert a base 10 number to any base in .NET?, but hardcoded to use your format (which is base 62).

    private static readonly char[] baseChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    private const long targetBase = 62;
    private const long maxNum = 62L*62*62*62*62*62 - 1;
    public static string NumberToString(long value)
    {
        if (value > maxNum)
            throw new ArgumentException();
        char[] result = "000000".ToCharArray();
    
        int i = result.Length - 1;
        do
        {
            result[i--] = baseChars[value % targetBase];
            value /= targetBase;
        } 
        while (value > 0);
    
        return new string(result);
    }
    
    0 讨论(0)
  • 2020-12-22 04:13

    A string-based approach, similar to Tim S's answer:

    private static string Increment(string input)
        {
            var myChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            var array = input.ToCharArray();
    
            for (int i = array.Count()-1; i >= 0; i--)
            {
                int newValue = (Array.IndexOf(myChars, array[i]) + 1) % 62;
                array[i] = myChars[newValue];
                if (newValue != 0)
                    break;
            }
            return new string(array);
        }
    
    0 讨论(0)
提交回复
热议问题