Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a character from the end of a string

Tags:

c#

I have a variety of strings in the following format:

WWSS1234519

S12319

etc.

All that I need to do is add a hyphen so that the 19 is separate. The above strings would become

This is C# in ASP.net, it's populating a gridview. Generic method with no insertions is:

 <asp:Label ID="AccLabel" runat="server" Text='<%# Eval("Acc") %>' Width="96px"/>

WWSS12345-19

S123-19

etc.

What I tried was finding the indexof the string based on "19" but that will change come new years and I don't want to have to update the code every year to get the hyphen to appear.

I know there's a simple way to do this - Just not sure how.

Thank you.

like image 960
Mikael Avatar asked Dec 07 '25 14:12

Mikael


1 Answers

If the year is always just the last two characters just do an insert:

var s = "WWSS1234519";
var formatted = s.Insert(s.Length - 2, "-");

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!