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

前端 未结 3 1374
自闭症患者
自闭症患者 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]);
                }
            }
        }
    
    }
    

提交回复
热议问题