[Solved] How to dynamically add SuperScript to number in ASP.NET/C# ? [closed]


Using the <sup> tag is the correct way to markup these values.

this i want to do dynamically in asp.net c#

If you have some knowledge of the locale, you could automatically add <sup> tags to text.

Matching

// this should go in a helper class

// Obviously this depends on locale. The regex can be altered to accept numbers
// with as many digits as desired. I think "th" is always an appropriate suffix
// in English (not sure).
private static readonly Regex _regex = new Regex( @"^(\d{1,8})(st|nd|rd|th)$", RegexOptions.Compiled );

public static string AddSuper( string value ) {
    return _regex.Replace( value, "$1<sup>$2</sup>" );
}

Usage

// in code-behind
this.litMyText.Text = AddSuper( "1st" );

// a few test cases (also demonstrates processing multiple items)

// should match
var testValues = new[] { "1st", "2nd", "10th", "20th", "1000th", "3rd", "19th" };

foreach( string val in testValues ) {
    Response.Write( AddSuper( val ) );
}

// should not match
testValues = new[] { "test", "nd", "fourth", "25", "hello world th", "15,things", "1 1 1thousand" };

foreach( string val in testValues ) {
    Response.Write( AddSuper( val ) );
}

Output on Matched Values

1<sup>st</sup>
2<sup>nd</sup>
10<sup>th</sup>
20<sup>th</sup>
1000<sup>th</sup>
3<sup>rd</sup>
19<sup>th</sup>

2

solved How to dynamically add SuperScript to number in ASP.NET/C# ? [closed]