Увеличение алфавитовC#

Место общения программистов C#
Ответить
Anonymous
 Увеличение алфавитов

Сообщение Anonymous »

Я пытаюсь создать функцию, которая будет выдавать мне позицию алфавита при передаче индекса. Это будет так же, как Excel показывает столбцы. A...Z, AA,AB.... Я написал функцию ниже, чтобы получить результаты до Z. Это выглядит так:

static string GetColumnName(int index)
{
const int alphabetsCount = 26;
if (index alphabetsCount)
{
int mod = index % alphabetsCount;
int columnIndex = index / alphabetsCount;

// if mod is 0 (clearly divisible) we reached end of one combination. Something like AZ
if (mod == 0)
{
// reducing column index as index / alphabetsCount will give the next value and we will miss one column.
columnIndex -= 1;
// passing 0 to the function will return character '@' which is invalid
// mod should be the alphabets count. So it takes the last char in the alphabet.
mod = alphabetsCount;
}
return GetColumnName(columnIndex) + GetColumnName(mod);
}
else
{
int code = (index - 1) + (int)'A';
return char.ConvertFromUtf32(code);
}
}


Подробнее здесь: https://stackoverflow.com/questions/924 ... -alphabets
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»