首页 > 解决方案 > Adding a character from the end of a string

问题描述

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.

标签: c#

解决方案


如果年份总是只是最后两个字符,只需插入:

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

推荐阅读