If you want a complete solution (which can format a string based on the '^' character), you will have to minimize it yourself. Or ... you can use the one I just rolled up for you:
Function to replace the input character with the corresponding superscript character (note that this is an extension function ):
public static char ToSuperscript( this char numericChar ) { switch ( numericChar ) { case '0': return '\u2070'; case '1': return '\u00B9'; case '2': return '\u00B2'; case '3': return '\u00B3'; case '4': return '\u2074'; case '5': return '\u2075'; case '6': return '\u2076'; case '7': return '\u2077'; case '8': return '\u2078'; case '9': return '\u2079'; default: return numericChar; } }
Now I like to use LINQ, so I need a special extension method to handle the scan (thanks for MisterMetaphor for directing me to this function ):
public static IEnumerable<U> Scan<T, U>( this IEnumerable<T> input, Func<U, T, U> next, U state ) { yield return state; foreach ( var item in input ) { state = next( state, item ); yield return state; } }
Custom extension function using LINQ to apply superscript formatting:
public static string FormatSuperscripts( this string unformattedString ) { return new string( unformattedString .ToCharArray() .Scan( ( state, currentChar ) => new { Char = state.IsSuperscript ? currentChar.ToSuperscript() : currentChar, IsSuperscript = ( currentChar >= '0' && currentChar <= '9' && state.IsSuperscript ) || currentChar == '^', IncludeInOutput = currentChar != '^' }, new { Char = ' ', IsSuperscript = false, IncludeInOutput = false } ) .Where( i => i.IncludeInOutput ) .Select( i => i.Char ) .ToArray() ); }
And finally, the function call:
string l_formattedString = "10^27 45^100".FormatSuperscripts();
Output:
10²⁷ 45¹⁰⁰
As already noted, the console does not display Unicode characters \ u2074 - \ u2079 correctly, but this function should work in scripts where the font supports these characters (for example, WPF, ASP.NET with a modern browser, etc.)
You can easily modify the above LINQ query to apply other formats, but I will leave this exercise to the readers.