简体   繁体   English

在 C# 中将字符串从数字 0-9 增加到小写 az 到大写 AZ

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

I want to be able to have a string 6 characters long starting with '000000'.我希望能够有一个以 '000000' 开头的 6 个字符长的字符串。 Then I want to increment it by one '000001' when I hit 9 I want to go to '00000a' when I get to z I want to go to '00000A'.然后我想在我点击 9 时将它增加 1 '000001' 我想在到达 z 时转到 '00000a' 我想转到 '00000A'。 When 'Z' is reached I want to reset the first to 0 and start with the next position '000010' So on and so forth.当到达“Z”时,我想将第一个重置为 0,然后从下一个位置“000010”开始,依此类推。 '000011','000012'...'0000a0','0000a1'...'0000A0','0000A1' '000011','000012'...'0000a0','0000a1'...'0000A0','0000A1'

How would I do this in C#?我将如何在 C# 中做到这一点?

Thank you in advance.先感谢您。 Mike麦克风

This uses the IntToString supporting arbitrary bases from the question Quickest way to convert a base 10 number to any base in .NET?这使用IntToString支持来自问题Quickest way to convert a base 10 number to any base in .NET? , but hardcoded to use your format (which is base 62). ,但硬编码以使用您的格式(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);
}

Here's another approach...it allows you to pass in a "starting revision" ("000000" in the example).这是另一种方法……它允许您传入“起始修订”(在示例中为“000000”)。 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.很久以前,我最初在 VB.Net 中编写了它,以回答一个具有非常具体要求的问题......所以下面的可能不是最有效的做事方式。

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

}

A string-based approach, similar to Tim S's answer:基于字符串的方法,类似于 Tim S 的回答:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM