Iterating through the Alphabet - C# a-caz
Edit: Made it do exactly as the OP's latest edit wants
This is the simplest solution, and tested:
static void Main(string[] args)
{
Console.WriteLine(GetNextBase26("a"));
Console.WriteLine(GetNextBase26("bnc"));
}
private static string GetNextBase26(string a)
{
return Base26Sequence().SkipWhile(x => x != a).Skip(1).First();
}
private static IEnumerable<string> Base26Sequence()
{
long i = 0L;
while (true)
yield return Base26Encode(i++);
}
private static char[] base26Chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private static string Base26Encode(Int64 value)
{
string returnValue = null;
do
{
returnValue = base26Chars[value % 26] + returnValue;
value /= 26;
} while (value-- != 0);
return returnValue;
}
Iterating through alphabet and get newly created strings back to their original Button Form
Your final code is in no way related to your variables. While there is a variable A
that represents a control, there is also an entirely separate collection within the form that has all of the Control
objects within it. Each of those control object has a Name
field, and one of them has a string value of "A"
. You can find the object in that collection with a name value of "A"
easily enough. That there happens to be a variable called A
that references that control is (from the point of view of the computer) a coincidence. If you renamed the variable (but didn't change the name of the control) to something like "A1"
, then you'd still need to use "A"
to get the control using the Controls
collection.
Quickest way to enumerate the alphabet
(Assumes ASCII, etc)
for (char c = 'A'; c <= 'Z'; c++)
{
//do something with letter
}
Alternatively, you could split it out to a provider and use an iterator (if you're planning on supporting internationalisation):
public class EnglishAlphabetProvider : IAlphabetProvider
{
public IEnumerable<char> GetAlphabet()
{
for (char c = 'A'; c <= 'Z'; c++)
{
yield return c;
}
}
}
IAlphabetProvider provider = new EnglishAlphabetProvider();
foreach (char c in provider.GetAlphabet())
{
//do something with letter
}
what is the best way to loop through alphabet in C# to output to Excel?
I was able to find another question on stackoverflow that had a working solution:
Here is the question: Fastest function to generate Excel column letters in C#
and the answer is the: ExcelColumnFromNumber() method
Iterating through the Alphabet - C# a-caz
Edit: Made it do exactly as the OP's latest edit wants
This is the simplest solution, and tested:
static void Main(string[] args)
{
Console.WriteLine(GetNextBase26("a"));
Console.WriteLine(GetNextBase26("bnc"));
}
private static string GetNextBase26(string a)
{
return Base26Sequence().SkipWhile(x => x != a).Skip(1).First();
}
private static IEnumerable<string> Base26Sequence()
{
long i = 0L;
while (true)
yield return Base26Encode(i++);
}
private static char[] base26Chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private static string Base26Encode(Int64 value)
{
string returnValue = null;
do
{
returnValue = base26Chars[value % 26] + returnValue;
value /= 26;
} while (value-- != 0);
return returnValue;
}
Changing a Variable A-Z if file exists(C#)
Okay, I am not clear on what exactly you are trying to accomplish. If you are trying to populate a couple of textboxes with a value based on the first file you find in a directory with a specific file name, then try the following:
void PopulateTextBoxes()
{
string format = "MLB028A-MTRSPR-B-{1}";
string format2 = "SPACER, {0}, CFG-{1} MLB028";
string fileName = @"C:\Engineering\Engineering\SW Automation\Linear Actuator Technology\MLC Series\Models\MLB028Z-MTRSPR-B-{1}.SLDPRT";
for(string start = "A"; start != "Z"; start = GetNextBase26(start))
{
if(File.Exists(String.Format(fileName,start)))
{
textBox3.Text = String.Format(format,start);
textBox5.Text = String.Format(format2,textBox3.Text,start);
break;
}
}
}
// CODE FROM http://stackoverflow.com/questions/1011732/iterating-through-the-alphabet-c-sharp-a-caz
private static string GetNextBase26(string a)
{
return Base26Sequence().SkipWhile(x => x != a).Skip(1).First();
}
private static IEnumerable<string> Base26Sequence()
{
long i = 0L;
while (true)
yield return Base26Encode(i++);
}
private static char[] base26Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private static string Base26Encode(Int64 value)
{
string returnValue = null;
do
{
returnValue = base26Chars[value % 26] + returnValue;
value /= 26;
} while (value-- != 0);
return returnValue;
}
Scheme - Converting Numbers to a Letter of Alphabet
http://docs.racket-lang.org/guide/characters.html
This part of the documentation explains it very clearly.
So what you can do is the following:
> (char->integer #\a)
97
> (char->integer #\b)
98
> (char->integer #\A)
65
Using this you can use a function as shown below:
(define (to-letter n)
(let ((charnum (+ n 96)))
(integer->char charnum)))
(define (to-int c)
(let ((charnum (char->integer c)))
(- charnum 96)))
> (to-int (to-letter 1))
1
Be sure to check for bounds etc.
Edit
To be more in the spirit of the given SO question, you could store the characters in an array and then you have O(1) indexing.
Incrementing an alphanumeric string
public enum Mode
{
AlphaNumeric = 1,
Alpha = 2,
Numeric = 3
}
public static string Increment(string text, Mode mode)
{
var textArr = text.ToCharArray();
// Add legal characters
var characters = new List<char>();
if (mode == Mode.AlphaNumeric || mode == Mode.Numeric)
for (char c = '0'; c <= '9'; c++)
characters.Add(c);
if (mode == Mode.AlphaNumeric || mode == Mode.Alpha)
for (char c = 'a'; c <= 'z'; c++)
characters.Add(c);
// Loop from end to beginning
for (int i = textArr.Length - 1; i >= 0; i--)
{
if (textArr[i] == characters.Last())
{
textArr[i] = characters.First();
}
else
{
textArr[i] = characters[characters.IndexOf(textArr[i]) + 1];
break;
}
}
return new string(textArr);
}
// Testing
var test1 = Increment("0001", Mode.AlphaNumeric);
var test2 = Increment("aab2z", Mode.AlphaNumeric);
var test3 = Increment("0009", Mode.Numeric);
var test4 = Increment("zz", Mode.Alpha);
var test5 = Increment("999", Mode.Numeric);
Related Topics
Use Task.Run() in Synchronous Method to Avoid Deadlock Waiting on Async Method
How to Ensure a Timestamp Is Always Unique
Do I Need to Lock or Mark as Volatile When Accessing a Simple Boolean Flag in C#
Replace Part of a JSON with Other (Using a String Token)
How to Set Web.Config File to Show Full Error Message
Methodinfo.Invoke with Out Parameter
Retrieve the Respective Coordinates of All Words on the Page with Itextsharp
How to Use Timezoneinfo to Get Local Time During Daylight Saving Time
The Name '...' Does Not Exist in the Current Context
Display Custom Error Page When File Upload Exceeds Allowed Size in ASP.NET MVC
Control Another Application Using C#
Differences Between Expandoobject, Dynamicobject and Dynamic
Conditionally Required Property Using Data Annotations
How to Create a C# App That Decides Itself Whether to Show as a Console or Windowed App