Solved

Take out the leading zero and 'F' form the string

Posted on 2003-10-28
4
212 Views
Last Modified: 2010-04-16
Hi All,
I'd like to have function to take out the string which has leading zero and 'F' character if appear but this function should only apply if the string is a number.
For example:
'F1123' ==> 1123
'F00012' ==> 12
'Fa12a' ==> Fa12a
'F0asd' ==> F0asd
'00123' ==> 123
'123AB' ==> 123AB
Thanks!
jpc
0
Comment
Question by:jpchen
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
_ys_ earned 125 total points
Comment Utility
This should do the trick:


using System;

string foo ( string sInput )
{
    char[] achExclude = {'f', 'F', '0'};

    string sTrimmed = sInput.TrimStart (achExclude); // exclude the leading 'junk'

    Int64 i64Result = 0;
    try
    {
        i64Result = Int64.Parse (sTrimmed); // convert to a number
    }
    catch (Exception)
    {
        return sInput; // not a number - return original string
    }

    return i64Result.ToString( );
}
0
 

Author Comment

by:jpchen
Comment Utility
I change to this, but I always got 0, why?

            private void button3_Click(object sender, System.EventArgs e)
            {
                        char[] achExclude = {'f', 'F', '0'};

                        string oText = textBox1.Text;
                        string sTrimmed = textBox1.ToString().TrimStart (achExclude); // exclude the leading 'junk'

                        Int64 i64Result = 0;
                        try
                        {
                              i64Result = Int64.Parse (sTrimmed); // convert to a number
                        }
                        catch (Exception)
                        {
                              textBox1.Text = oText;
//                              return sInput; // not a number - return original string
                        }
                        textBox1.Text = i64Result.ToString( );
            }
0
 

Author Comment

by:jpchen
Comment Utility
I found out what problem is.
Thanks!
jpc
0
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
Even though the Q is already closed, you could have this with a one-liner:
using System.Text.RegularExpressions;

    string trimmed=Regex.Match(input, @"^(F?0*(?<value>[1-9]\d*)|(?<value>.*))$").Groups["value"].Value;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

744 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

18 Experts available now in Live!

Get 1:1 Help Now