Remove penultimate character in a string

Posted on 2007-08-09
Last Modified: 2013-11-05

As part of project I am working on I have to remove the penultimate digit from a six digit number (which I will not know). For example ,the number 922001 needs to become 92201.  It would seem to me that the best way to achieve this is by using string builder, but I can't get my string builder code achieve my goal.

Can anyone help?
Question by:JakeyCakes
    LVL 3

    Accepted Solution

    Does this snippet work for you?

    int number_in = 123456;
    string strNumber = number_in.ToString();
    while (strNumber.Length < 6) strNumber = "0" + strNumber;  // when the number starts with a 0
    strNumber = strNumber.Substring(0, 4) + strNumber.Substring(5);
    int number_out = int.Parse(strNumber);
    System.Windows.Forms.MessageBox.Show(number_out + "");

    Author Comment


    You genius! It does what I want perfectly, and it seems so much more efficient than my idea I had after posting this question (I thought about using the Remove function in the stringbuilder class). The points are deservedly yours.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now