"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:uu8ntQOuDHA.2304@TK2MSFTNGP12.phx.gbl...[color=blue]
> Eric,
>
> Instead of converting it to an array of integers, why not convert it[/color]
to[color=blue]
> an array of characters (char)? The char type is a unicode character, so[/color]
it[color=blue]
> can contain the value easily. Once you have that, you modify it and then
> pass the array back to the string constructor to get your string back.
>
> Also, if you are performing a lot of operations, you might want to[/color]
store[color=blue]
> the character values in a StringBuilder instance.
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> -
mvp@spam.guard.caspershouse.com[/color]
The only reason I didn't use char is that I'm performing an addition
operation with the char.
Basically, I'm generating incrementing a string like the column numbering
system in Excel. A,B,C...,Z,AA,AB,AC...,AZ,AAA....
This may not be the best way, but it now works.
/// <summary>
/// Take a string generated by this sub, and figure the next logical
/// item in the series.
/// </summary>
/// <param name="current">The string we are 'incrementing' </param>
/// <returns>The next string in the series. Ex: A returns B, CC returns
CD, ZZ returns AAA</returns>
public static string BuildNextAlphabeticCode(string current)
{
//Convert argument to array of ints
int[] codes = General.StringHelp.UnicodeValues(current);
bool done = false;
for(int i = codes.GetUpperBound(0); i >= codes.GetLowerBound(0); i--)
{
int incremented = codes[i] + 1;
if(incremented <= MAX_CHAR_CODE)
{
codes[i] = incremented;
done = true;
break;
}
else
{
//set this one back to min char code
codes[i] = CAP_A_CHAR_CODE;
}
}
//convert back to string
char[] chars = new char[codes.GetUpperBound(0)];
StringBuilder sb = new StringBuilder();
for(int j = codes.GetLowerBound(0); j <= codes.GetUpperBound(0); j++)
{
sb.Append(Convert.ToChar(codes[j]));
}
if(!done)
{
//The string needs to be one character longer.
//add a character
//CAP_A_CHAR_CODE;
sb.Append(Convert.ToChar(CAPA_CHAR_CODE));
}
return sb.ToString();
}
Is there a much better way?
Thanks for the reply,
Eric