string setting in textbox

Hi I have a text ox that display the following string : A0123
I want it to display this string that way: A01-2-3
How to acheive this?

Best regards,
pascalmartinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

try this

string input = "A0123";
input = input.Insert(3, "-").Insert(5, "-");
TextBox1.Text = input;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pascalmartinAuthor Commented:
The box returned empty with the following message created by the line number 2.

Error accessing database.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex
mscorlib at System.String.Insert(Int32 startIndex, String value) at DeliShop.ProductView.DoData() in C:\Users\pascal.PREMIERRETAIL\Documents\Visual Studio 2008\Projects\DeliShop\DeliShop\UControls\ProductView.ascx.vb:line 80
pascalmartinAuthor Commented:
Sorry,
Forget the error message, I attached the code to the wrong box.
However the correct box still return empty.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

pascalmartinAuthor Commented:
Its Working now, thanks!
pascalmartinAuthor Commented:
I have an issue with the code after refreshing the page and moving to the next record, I receive the following error message:
Error accessing database.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex
mscorlib
Lokesh B RDeveloperCommented:
Hi,

If your input length is not matching with the INDEX, you get the error.
So check the length is greater than the replacing character and continue.

string input = "A0123";

            if (input.Length > 3)
            {
                input = input.Insert(3, "-");
            }
            if (input.Length > 5)
            {
                input = input.Insert(5, "-");
            }
TextBox1.Text = input;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.